homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Htaccess for multiple Domains
redirecting non-www to www

 1:08 am on Jan 28, 2009 (gmt 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)

# 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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved