Welcome to WebmasterWorld Guest from

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)

10+ Year Member

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)

WebmasterWorld Senior Member 5+ Year Member

# 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".


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


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

10+ Year Member

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

Featured Threads

Hot Threads This Week

Hot Threads This Month