Reputation: 2615
I've setup a number of htaccess rules to redirect a large number of old domains to one new domain. Each old domain points to a specific page on the new website. For example:
olddomainlocation1.com
redirects to newdomain.com/location1
olddomainlocation2.com
redirects to newdomain.com/location2
etc.
The rewrite rule redirects as expected if you visit an old domain name without any path in the url, but when I visit an old domain with a path in the url it doesn't work.
eg. olddomainlocation1.com/index.html
, my rewrite rule doesn't work, I just end up on newdomain.com/index.html
(which doesn't exist)
Here is an example of my htaccess rule. Any ideas where I'm going wrong?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{http_host} ^www.olddomainlocation1.co.uk [NC,OR]
RewriteCond %{http_host} ^olddomainlocation1.co.uk [NC]
RewriteRule ^(.*)$ https://newdomain.co.uk/location1/ [R,NC,L]
</IfModule>
UPDATE
I have updated the rule now which seems to be closer to working, but it's not quite there yet.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomainlocation1.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^olddomainlocation1.co.uk$ [NC]
RewriteRule ^ https://newdomain.co.uk/location1/ [R,NC,L]
</IfModule>
Adding $ to the end of my RewriteCond makes sure the rule runs on pages like /index.html.
When I test this rule on https://technicalseo.com/tools/htaccess/ and it's telling me that the redirect should be in place, but when I test the redirect, the url isn't changing for me in my browser.
UPDATE 2
Further update: I do have WordPress specific rules above the domain redirects, which could be causing an issue, here is that code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^newdomain.co.uk$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Upvotes: 1
Views: 256
Reputation: 45829
Further update: I do have WordPress specific rules above the domain redirects, which could be causing an issue
Yes, that is certainly the problem. The WordPress specific rules (ie. the WordPress front-controller) should appear later in the file. The external redirect needs to go near the top of the .htaccess
file, before the WordPress front-controller. Order matters.
The problem is that a request of the form /index.html
(which does not exist) is rewritten to the WordPress engine before your redirect (that appears later) is able to process the request. So this will ultimately trigger a WordPress generated 404 instead of a redirect.
For example:
# Redirect olddomain to page on newdomain
RewriteCond %{HTTP_HOST} ^(www\.)?olddomainlocation1\.co\.uk [NC]
RewriteRule ^ https://newdomain.co.uk/location1/ [R=302,L]
# BEGIN WordPress
# :
Aside: I've combined your 2 conditions into a single RewriteCond
directive.
You do not need the <IfModule>
wrapper nor repeat the RewriteEngine
directive.
Upvotes: 1