Forum Moderators: phranque

Message Too Old, No Replies

Redirecting numerous domain versions to 1 domain

Is this code correct?

         

The Cricketer

2:37 pm on Aug 31, 2005 (gmt 0)

10+ Year Member


Can someone confirm that the htaccess code below is good to do the following:

I need to redirect

www.mydomain.com
mydomain.com
mydomain.co.uk

to www.mydomain.co.uk

The redirects need to be permanent redirects which will not confuse the search engine spiders.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)mydomain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mydomain\.co.uk$ [NC]
RewriteRule ^.*$ http://www.mydomain.co.uk%{REQUEST_URI} [R=301,L]

jd01

6:19 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about using a negative pattern and catching them all?

RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk
RewriteRule (.*) http://www.mydomain.co.uk/$1 [R=301,L]

Justin

The Cricketer

8:46 am on Sep 1, 2005 (gmt 0)

10+ Year Member



excellent thanks that seems a more sensible approach. It would be ideal if everything except for www.mydomain.co.uk/ was redirected to www.mydomain.co.uk/

At the moment www.mydomain.co.uk doesn't redirect to www.mydomain.co.uk/ (i.e. with the forward slash)

Am i right in thinking that this is not a good idea because it will create a loop?

jd01

5:23 pm on Sep 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, Apache does this anyway, some browsers do not show the / but that is where the file is coming from. And, if not yoursite.com and yoursite.com/ are duplicates...

You should not have any problem with looping - It only redirects if there is HTTP_HOST information (HTTP 1.0 clients do not send HOST headers) AND the HTTP_HOST is not already www.yoursite.co.uk, so the ruleset will break itself out of a loop.

Justin