Forum Moderators: phranque

Message Too Old, No Replies

consolidating urls problem

htaccess, mod-rewtite, fasthosts, multiple urls

         

bazil

4:18 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



Hi Guys

Im trying to consolidate all my urls for one site onto the one, main url. for example www.example.com is to be the main one with example.com, www.my-site.com, my-site.com, mysite.co.uk, my-site.co.uk all pointing to that main url

the reason for this is that Google is listing half of my site on one url and half on an other! no good!

i am trying to use these lines in my .htaccess but im getting 500 errors from the re-routed domains when i access them

if anyone can help with this it would be great, its hosted with fasthosts who suport i have exasted on this one

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?my-site\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?my-site\.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^my-site\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^mysite\.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^my-site\.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

thanks very much for any light you can throw my way on this one as my site's traffic is falling apart!

Regards,
Barry

[edited by: jdMorgan at 10:56 pm (utc) on Aug. 23, 2007]
[edit reason] Example.com [/edit]

jdMorgan

5:35 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing really wrong with that code. So, the next step is to look at your server error log after testing some of the domain variants that give a 500-Server Error; The information in the error log often tells you exactly what is wrong.

For example, you may not be allowed to use the Options directive, depending on your host. If you are allowed to use it, it might be required or it might be unnecessary, again depending on your host.

You may not even be allowed to use mod_rewrite on some really-lousy hosts. The only way to determine these cases is to construct simple tests for each case. Be sure to completely flush your browser cache before testing any change to your code.

The only --and very minor-- problem I see is that you've got several instances of the incompletely-escaped pattern "...site\.co.uk", which should read "...site\.co\.uk". However, that won't cause a 500-Server Error.

Also, have you considered using just one rule to redirect all non-canonical domain requests?


# Externally redirect if NOT canonical domain
RewriteCond %{HTTP_HOST} [b]!^w[/b]ww\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

g1smd

7:42 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The last three rules are not needed at all.

The (www\.)? in the first three rules means that they already work for both www and non-www URLs.

The fourth rule (correctly) only needs a rule for non-www requests.

You can use my(-)?site to make it match for both my-site and mysite.

You can also use \.co(m¦\.uk) to make it match for .com and .co.uk too.

.

... Or you could go with the !NOT one-liner that jd used.

bazil

9:23 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



Brilliant!
Thanks so much jd and g1!

everything now appears to be working well and im delighted :)

i removed the Options directive and went with the single rule as Jd proposed and hey presto!

so, for anyone else having a similar problem on Fasthosts Linux servers, here is the solution (thanks again gentlemen!)

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

[edited by: jdMorgan at 10:52 pm (utc) on Aug. 23, 2007]
[edit reason] Corrected formatting as noted below. [/edit]

g1smd

9:26 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to place a space before the ! in the real code.

The forum software breaks that piece of example code when posted here.

I almost never use that one-liner "catch all" redirect. Often there are multiple sites on the server and I like to have one line of stuff for each one.

bazil

9:32 pm on Aug 23, 2007 (gmt 0)

10+ Year Member



Thanks again G1

i do have that space in my code, just not showing here so watch out for that anyone using that code!

one thing, after reading up all i can about this im hoping that this change will not only clean up the obvious problems that multiple urls in google present, but also shift all the pr from those other urls onto my main one. has this been your experience when doing this? how long does it take? and surely i wont end up with a pr8 site after my 2 pr4 urls have been combined will i? (he says in a childlike wonder sort of way!)

cheers lads

Barry

g1smd

9:49 pm on Aug 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The PR takes usually weeks to months to consolidate.

It shows on the Toolbar several months after that.