Server (WordPress) redirects files that are not supposed to (using htaccess)

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
# :

Leave a Comment