Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite every subdomain to www



7:38 pm on Dec 2, 2008 (gmt 0)

10+ Year Member


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!


12:25 am on Dec 3, 2008 (gmt 0)

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

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


12:54 am on Dec 3, 2008 (gmt 0)

5+ Year Member

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


5:53 am on Dec 3, 2008 (gmt 0)

10+ Year Member

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!


12:45 am on Dec 4, 2008 (gmt 0)

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

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.



Featured Threads

Hot Threads This Week

Hot Threads This Month