Forum Moderators: phranque
Would appreciate some assistance with redirects. I’ve successfully competed a non(www) to www redirect with the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.co.uk [NC]
RewriteRule ^(.*)$ [mydomain.co.uk...] [L,R=301]
So far so good… Now I have a 2nd domain, ‘www.mydomain.com’, that points to the ‘.co.uk’ server.
I would now like to permanently redirect the ‘.com’ to the ‘.co.uk’ as described below:
Redirect these…
‘www.mydomain.com’
‘mydomain.com’
To…
‘www.mydomain.co.uk’
Can this be done in the same .htaccess file as the one above? The reason for wanting to do this is to avoid dup content issues with the search engines.
Thanks
The Swede
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk\:[0-9]+ [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
Another way to do it -- and actually more efficient if you can use it, is to simply redirect any requested hostname that is NOT exactly the canonical domain (or blank) to that canonical domain:
RewriteCond %{HTTP_HOST} !^(www\.example\.co\.uk)?$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
Jim