I have three domains redirecting to a fourth. The .htaccess code for each is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain\\.co\\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\\.olddomain\\.co\\.uk$
RewriteRule ^/?$ "http\\:\\/\\/www\\.newdomain\\.co\\.uk" [R=301,L]
This works fine for two domains, which have never been used for a site, but there’s a problem with the third. Until recently, there was a site there, although for the last few months it just consisted of a single page. (www.)olddomain.co.uk redirects correctly to [noparse]www.newdomain.co.uk[/noparse], but old links which went to www.olddomain.co.uk/index.html, while they show the new site, still display the old URL in the address bar.
What have I missed?
TB,
I believe that you’ve insisted that index.html NOT be in the {REQUEST_URI} variable.
Try:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\\.)?.olddomain\\.co\\.uk [NC]
RewriteRule .? http://www.newdomain.co.uk [R=301,L]
The changes I made to your code (and the reasons) were:
- It’s easy to combine the RewriteCond regex to account for the www’d and non-www’d domains.
- The {HTTP_HOST} variable is case sensitive while the domains are not - the No Case flag resolves this problem.
- Your code insisted on NOTHING in the {REQUEST_URI} (except the / for ancient servers - pretty safe to ignore the leading /'s these days). I used .? to yield a TRUE value for every request (to olddomain.co.uk).
- PLEASE, don’t use \'s to escape characters in a redirection! While they won’t yield an error, they’re superfluous and make the code look ridiculous.
Regards,
DK
Thanks, DK - that’s great.
I actually set up the redirects through cpanel, so that’s the code it generated. I’ll know better for next time. 
Edit: Sorry, should have tried that before replying. It’s actually made the problem worse. Now all three domains display the wrong URL. 
It may be just a stray period.
Before:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?.olddomain\.co\.uk [NC]
RewriteRule .? http://www.newdomain.co.uk [R=301,L]
After:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.co\.uk [NC]
RewriteRule .? http://www.newdomain.co.uk [R=301,L]
And so it proved, because that works perfectly, thank you. 