1

I am currently moving a domain from one to the other, this is not an HTTPS move but rather a move from exampleone.co.uk to exampletwo.co.uk. I am using this code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exampleone\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.exampleone\.co.uk$
RewriteRule (.*)$ http://www.exampletwo.co.uk/$1 [R=301,L]
</IfModule>

This works well for existing page names as the site structure isn't changing. However it also returns non existing pages to the index page basically creating a soft 404 which is not desirable.

Does anybody have any idea how to redirect valid pages from exampleone.co.uk via 301 but returning a 404 when the page doesn't exist but also taking into account valid 301's within the new domain.

So we want it to:

  1. Redirect via 301 old site to new
  2. Honour valid 301's within the new site (future proofing)
  3. Return 404 instead of redirecting to index page when 301 does not exist
  • "it also returns non existing pages to the index page basically creating a soft 404 which is not desirable" - that would seem to be a fault of the new site? So, you're saying that http://www.exampletwo.co.uk/this-page-does-not-exist does not return a 404? That would be something you need to fix on the new site. Do these two domains point to the same filesystem? – MrWhite Jun 14 at 11:33
  • 1
    "That would be something you need to fix on the new site. Do these two domains point to the same filesystem?" - yep spot on... it seems to be an issue with the new site. Only change is version update of site software. Some investigation needed here. Thanks – Spike Jun 14 at 11:48
0

it also returns non existing pages to the index page basically creating a soft 404 which is not desirable.

That would seem to be a fault of the new site that you would need to fix as a separate issue. This is not a fault of (or something that can be fixed with) your .htaccess directives.

Non-existent URLs on the new site are simply not returning a 404, when they should be.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exampleone\.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.exampleone\.co.uk$
RewriteRule (.*)$ http://www.exampletwo.co.uk/$1 [R=301,L]
</IfModule>

Just a few notes regarding your existing directives:

  • The <IfModule> wrapper is not required. This will only function as intended when mod_rewrite is available, so the <IfModule> check should be omitted.

  • (.*)$ - The trailing $ on the RewriteRule pattern is superfluous. Regex is greedy by default and you have already omitted the ^ (start-of-string-anchor) from the beginning of the regex.

  • Maybe just in your example, but you have failed to escape the literal dot in co.uk in both the CondPatterns.

  • You only need one condition, instead of two:

    RewriteCond %{HTTP_HOST} ^(www\.)?exampleone\.co\.uk [NC]
    

    Note that I've removed the OR flag and added the NC flag (to catch malformed requests). I've also omitted the trailing $ so as to be able to match FQDN, that end in a dot.

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.