1

I use .htaccess to redirect some addresses or avoid some addresses to be redirected. Basically, I have all things redirected to another domain except for certain addresses that I specified. So example.com/NormalPage will be redirected to example.dev/NormalPage but example.com/ThatPage won't be redirected and if someone opens example.dev/ThatPage, it'll be redirected to example.com/ThatPage. Some addresses are also accessible from both domains. For example, example.com and example.dev won't get redirected and both show same thing.

Now, because of this redirection, wp-admin and other WordPress filesystems are probably (not sure) getting redirected. My main domain is example.com not example.dev but now I have to post notes or edit pages using example.dev (which is the new domain). That shouldn't happen.

I want both domains to be able to post and edit pages and writings. Now, when I want to edit a page or post a note, when I press the publish button or update button, it tells me that I'm offline or when I want to select a category for a post, it doesn't list them.

Here's my current .httaccess file. Can you tell me how to fix this?

# Prevent rewritten requests (to the WP front-controller) from being redirected
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]

# The TARGET_DOMAIN environment variable holds the desired target domain (if any)
#  - for the requested URL
# eg. "example.com" or "example.dev" or empty for no redirect / accessible from both.

# Set the "default" target domain
#  - Any URLs not listed below will redirect to this domain
RewriteRule ^ - [E=TARGET_DOMAIN:example.dev]

# URLs that should be redirected to (or remain at) the other domain
RewriteCond %{REQUEST_URI} ^/bio [OR]
RewriteCond %{REQUEST_URI} ^/computing [OR]
RewriteCond %{REQUEST_URI} ^/contact [OR]
RewriteCond %{REQUEST_URI} ^/donate [OR]
RewriteCond %{REQUEST_URI} ^/encrypt [OR]
RewriteCond %{REQUEST_URI} ^/genderless-pronouns [OR]
RewriteCond %{REQUEST_URI} ^/gnu-linux-controversy [OR]
RewriteCond %{REQUEST_URI} ^/keys [OR]
RewriteCond %{REQUEST_URI} ^/legal [OR]
RewriteCond %{REQUEST_URI} ^/pages [OR]
RewriteCond %{REQUEST_URI} ^/readings [OR]
RewriteCond %{REQUEST_URI} ^/now
RewriteRule ^ - [E=TARGET_DOMAIN:example.com]

# URLs that should not be redirected - accessible from both domains
#  - Sets TARGET_DOMAIN to empty string (ie. no target domain)
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-login [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-content [OR]
RewriteCond %{REQUEST_URI} ^/wp-includes [OR]
RewriteCond %{REQUEST_URI} \.(php|css|js|jpg|gif|webp)$ [OR,NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [E=TARGET_DOMAIN]

# Redirect to the desired TARGET_DOMAIN (if any)
#  - if not already at the TARGET_DOMAIN
RewriteCond %{ENV:TARGET_DOMAIN} .
RewriteCond %{HTTP_HOST}@@%{ENV:TARGET_DOMAIN} !^([a-z0-9.-]+)@@\1$
RewriteRule ^ https://%{ENV:TARGET_DOMAIN}%{REQUEST_URI} [R=302,L]

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 503 /503.html

1 Answer 1

0

I have all things redirected to another domain except for certain addresses that I specified

This is most certainly the issue here. There will be additional HTTP requests that would need to be added to the following rule block in order to prevent requests intended for example.com being redirected to example.dev.

# URLs that should not be redirected - accessible from both domains
#  - Sets TARGET_DOMAIN to empty string (ie. no target domain)
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-login [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-content [OR]
RewriteCond %{REQUEST_URI} ^/wp-includes [OR]
# Add more conditions here...
RewriteCond %{REQUEST_URI} \.(php|css|js|jpg|gif|webp)$ [OR,NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [E=TARGET_DOMAIN]

However, in order to determine these additional URLs to exclude, you'll need to examine the HTTP requests being made in the browser. Some of these requests might be triggered by JavaScript/AJAX. I don't off-hand know what these requests would be unfortunately, but it could possibly vary depending on plugins etc.


Alternatively, you change your approach and instead of redirecting "everything" from example.com to example.dev (the default action) you only redirect select URLs. For example:

  • Some page URLs redirect to (or remain at) example.com
  • Some page URLs redirect to (or remain at) example.dev
  • Any URL not specified does not redirect.

In .htaccess:

# Prevent rewritten requests (to the WP front-controller) from being redirected
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]

# The TARGET_DOMAIN environment variable holds the desired target domain (if any)
#  - for the requested URL
# eg. "example.com" or "example.dev" or empty for no redirect / accessible from both.

# The "default" is NO REDIRECT

# URLs that should be redirected to (or remain at) example.com
RewriteCond %{REQUEST_URI} ^/bio [OR]
RewriteCond %{REQUEST_URI} ^/computing [OR]
RewriteCond %{REQUEST_URI} ^/contact [OR]
RewriteCond %{REQUEST_URI} ^/donate [OR]
RewriteCond %{REQUEST_URI} ^/encrypt [OR]
RewriteCond %{REQUEST_URI} ^/genderless-pronouns [OR]
RewriteCond %{REQUEST_URI} ^/gnu-linux-controversy [OR]
RewriteCond %{REQUEST_URI} ^/keys [OR]
RewriteCond %{REQUEST_URI} ^/legal [OR]
RewriteCond %{REQUEST_URI} ^/pages [OR]
RewriteCond %{REQUEST_URI} ^/readings [OR]
RewriteCond %{REQUEST_URI} ^/now
RewriteRule ^ - [E=TARGET_DOMAIN:example.com,S=1]

# URLs that should be redirected to (or remain at) example.dev
RewriteCond %{REQUEST_URI} ^/example-one [OR]
RewriteCond %{REQUEST_URI} ^/example-two [OR]
RewriteCond %{REQUEST_URI} ^/example-three
RewriteRule ^ - [E=TARGET_DOMAIN:example.dev]

# Redirect to the desired TARGET_DOMAIN (if any)
#  - if not already at the TARGET_DOMAIN
RewriteCond %{ENV:TARGET_DOMAIN} .
RewriteCond %{HTTP_HOST}@@%{ENV:TARGET_DOMAIN} !^([a-z0-9.-]+)@@\1$
RewriteRule ^ https://%{ENV:TARGET_DOMAIN}%{REQUEST_URI} [R=302,L]

# BEGIN WordPress
# :
1
  • Well all posts need to be redirected so specifying every URL that should be redirected is not an option.
    – ARH
    Nov 10, 2020 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.