1

I am using a wildcard redirect to redirect my old domain to a new domain. It works great but misses a few URLs (they don't get redirected to the destination and return status 200)

Can you please help me understand what's wrong with this? Maybe I am doing the wildcard redirect incorrectly or it's a server issue. My old domain is old.example and my new domain is new.example.

Example URL which isn't getting redirected: old.example/example-url

Here's my .htaccess file:

# BEGIN WordPress
<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

#Options +FollowSymLinks

# Redirect everything
RewriteRule ^(.*)$ https://new.example/$1 [R=301,L]
  • Is you new domain on a different server? – MrWhite Apr 24 at 17:52
  • No @MrWhite, it's on the same server. Will that cause any issue? – Naser Mohd Baig Apr 24 at 18:01
1

You've put the redirect directive in the wrong place. It needs to go before the WordPress front-controller, otherwise, the redirect will simply get ignored for anything other than URLs that map directly to the filesystem.

Since these domains are also on the same server (same hosting account I assume) then you will need to check for the requested hostname, otherwise, you'll get a redirect loop.

For example:

# Redirect everything from old to new domain
RewriteCond %{HTTP_HOST} ^old\.example [NC]
RewriteRule (.*) https://new.example/$1 [R=301,L]

# BEGIN WordPress
# :

The regex ^(.*)$ can be simplified to (.*) since regex is greedy by default.

| improve this answer | |
  • Thank you for the fix, @MrWhite. Could you please explain more about the "URLs that map directly to the filesystem"? How come some article URLs work and some doesn't as all of them should belong to the same redirect group I believe? – Naser Mohd Baig Apr 24 at 18:17
  • "URLs that map directly to the filesystem" - The WordPress code block sends all requests for "virtual" URLs to index.php (the front-controller) - processing of .htaccess then stops (the L flag) and WordPress handles the request. However, URLs that map directly to actual files and directories (eg. images, css, js, etc) are ignored by the WordPress directives in .htaccess and so processing continues to the directives (ie. the redirect) that follows. So, the redirect would only be seen for "virtual" (WordPress) URLs. – MrWhite Jun 1 at 12:16
  • "How come some article URLs work and some doesn't" - That is a bit of a mystery without knowing more about your system. Once you have ruled out any caching issues then all the "article URLs" (assuming they are "virtual" WP-only URLs) are processed by WordPress - so if you are seeing a difference in behaviour then it's because WP itself is handling these URLs differently for some reason. If you are seeing a redirect then it would seem WP itself is issuing the redirect; not .htaccess. Do these URLs exist at old.example? If you see a 200 OK response then it would seem that they do. – MrWhite Jun 1 at 12:32
  • 1
    Yes @MrWhite, those URLs did returned a status 200 (well before I applied the fix you suggested) and no, I don't have a redirection plugin active. Either way, the redirection is now working without any flaws :) – Naser Mohd Baig Jun 1 at 18:31
  • 1
    CORRECTION to my last sentence on my first comment above... "So, the redirect would only be seen for "virtual" (WordPress) URLs." - errrm, that's the opposite of what I meant and contradicts with everything that precedes it! There should be a "not" in there, as in: "...the redirect would not be seen for "virtual" (WordPress) URLs.", or alternatively, "...the redirect would only be seen for requests that map to physical files." (Sorry for any additional confusion! Glad it's now working as intended.) – MrWhite Jun 1 at 22:27

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.