homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite every subdomain to www

5+ Year Member

Msg#: 3798446 posted 7:38 pm on Dec 2, 2008 (gmt 0)


I am using wildcard subdomains for my site but I want to rewrite all of them to www in apache and can't quite get the code to work. I keep getting a "too many rewrites" error like it is going in a loop.

An example would be:

subd1.mydomain.com -> www.mydomain.com
subdx.mydomain.com -> www.mydomain.com
asd3f.mydomain.com -> www.mydomain.com

I had the following:

RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.net$
RewriteRule ^/(.*) [mydomain.net...] [R=Permanent,L]

Thanks for any pointers!



WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 3798446 posted 12:25 am on Dec 3, 2008 (gmt 0)

Yes, there will be a loop, as www will redirect to www.

You need another RewriteCond that says "only do this for NOT www".

If this code is for .htaccess the leading slash in the Rule must be omitted. For http.conf include it as per your example.

By the way you called this a "rewrite" but in fact it is a "redirect". Big difference :-)


5+ Year Member

Msg#: 3798446 posted 12:54 am on Dec 3, 2008 (gmt 0)

Does it make any difference where you insert the additional RewriteCond?


5+ Year Member

Msg#: 3798446 posted 5:53 am on Dec 3, 2008 (gmt 0)

Ok so I need to have something like the following:

RewriteCond %{HTTP_HOST) !^www\.mydomain\.net
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.net$
RewriteRule ^/(.*) [mydomain.net...] [R=Permanent,L]

When I do that it gives me an error and takes me to mydomain.net//////////

Also note I am putting the above in httpd.conf not .htaccess

Thanks for the help!


WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 3798446 posted 12:45 am on Dec 4, 2008 (gmt 0)

It looks like you copied this new RewriteCond from another relatively-recent thread here, and it suffers from the same problem I identified in that thread: The original author attempted to close the first RewriteCond variable with ")" instead of the required "}" -- That is, with a right parenthesis instead of the required "right-squiggly-bracket."

Also, for efficiency, you should consider using a more-specific subpattern in the second RewriteCond, and getting rid of things that you don't need, such as the back-reference to the subdomain:

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

Note also that I deleted the end-anchor from the second RewriteCond's pattern. Left in place, your rule could be defeated by an FQDN hostname request, or one with an appended port number.

If you also want to redirect all possible sub-subdomains as well as subdomains, then you can code that as:

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

And you can then also redirect http://example.com to www.example.com by changing only the quantifier:

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

However, then we devolve to the case of "redirect requests for any hostname except the exact canonical www.example.com to www.example.com" -- more simply coded as:

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

To be clear, requests for an FQDN hostname or a hostname plus a port number are perfectly-valid, but obviously non-canonical in most cases. So your rule should correctly-handle requests for www.example.com./foo.html, www.example.com:80/foo.html, or www.example.com.:80/foo.html.


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