404 pages from the old site end up redirecting to the home page of the new site, why?
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, 404, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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:
- Redirect via 301 old site to new
- Honour valid 301's within the new site (future proofing)
- 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 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 theRewriteRulepattern 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.ukin 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
ORflag and added theNCflag (to catch malformed requests). I've also omitted the trailing$so as to be able to match FQDN, that end in a dot.
Comments
Post a Comment