Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Htaccess for multiple Domains

redirecting non-www to www

     
1:08 am on Jan 28, 2009 (gmt 0)

New User

10+ Year Member

joined:June 1, 2004
posts:33
votes: 0


Hi apache gurus,

Let's say that you got 30 domain names pointing to 1 IP address and that you want to redirect all the non-www versions of your domains to their www versions. Now let's suppose that you are very lazy, and that you don't want to add the *classical* redirect code for each domain but try to automatize it and say that by default, no matter the domain name, automatically redirect it to his www version... Is it just a dream or there is hope ?

Here is what I'd like to avoid :

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

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

...

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

Let me know your thoughts !
Thanks in advance

6:53 am on Jan 28, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 2, 2006
posts:2112
votes: 2


# canonicalize all non-www domain variants
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9][a-z0-9\-]*[a-z0-9]\.(co\.[a-z]{2}[a-z]{2,6}))\.?(:[0-9]{1,5})?$ [NC]
RewriteRule (.*) [%1...] [R=301,L]
#
# canonicalize all www domain variants
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9][a-z0-9\-]*[a-z0-9]\.(co\.[a-z]{2}[a-z]{2,6}))(\.\.?:[0-9]{1,5})$ [NC]
RewriteRule (.*) [%1...] [R=301,L]

The above is "made by Jim".

[webmasterworld.com...]

Replace the broken pipe "" characters in all code above with solid pipes before use.

2:49 pm on Jan 28, 2009 (gmt 0)

New User

10+ Year Member

joined:June 1, 2004
posts:33
votes: 0


Worked like a charm !
Thanks a lot, you saved me a lot of headaches !