Forum Moderators: phranque

Message Too Old, No Replies

.htaccesss with non (www) + TLD domain permanent redirects

Permanent redirects for non (www) + TLD domain with .htaccess file

         

the_swede

10:29 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Hi there,

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

jdMorgan

12:33 am on Feb 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, you can use the same rule, in fact:

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]

The two additional RewriteConds for "www.example.co.uk." and "www.example.co.uk:<numbers>" are to canonicalize FQDN requests and requests with a port number appended to the hostname, either or both of which are perfectly-valid but non-canonical.

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]

The blank hostname is accepted in case your server is accessible by its IP address. In this case, you may see occasional requests from very old HTTP/1.0 clients, which do not send the HTTP Host header. If this happens, failure to accept the blank hostname would result in an 'infinite' redirection loop.

Jim

the_swede

9:05 am on Feb 18, 2009 (gmt 0)

10+ Year Member



Jim,

You are a star - your first suggestion did the trick! I was sitting experimenting last night for hours with no luck. Need less to say I'm not an server expert!

Many thanks,

Happy Swede