Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite Question.httpd.conf - no subdomains

6:33 pm on Jun 9, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 16, 2004
posts: 58
votes: 0

Hi, I have succesfully implemented some rewrite code in my http.conf file, for redirecting pages to the www.domain.com site instead of the non domain.com site.

I am wondering if anyone could help me with this code to add something that says: do not redirect to the www.domain.com version of the page if the domain is a subdomain ie: test.domain.com

So, I'd like to retain my redirects for
domain.com goes to www.domain.com
but no redirects if it is subdomain.domain.com

Could anyone help me with this please? Thank you!

Here is my generic code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^/(.*)$ [%{HTTP_HOST}...] [R=301,L]

10:24 pm on June 9, 2008 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
votes: 0

Your code, as shown above, will not redirect requests for subdomains. The RewriteCond pattern says, "Match one or more characters starting with or containing anything but a period, followed by a period, followed by and ending with one or more characters which are anything but a period. In other words, only one period is allowed, otherwise the RewriteCond will not match and the rule will not be applied.

You might want to look around in the other config files, as it's possible that another rule is responsible for the unexpected redirection of your subdomains.

There's also one latent defect in the rule above: It could be unexpected defeated by perfectly-valid, but non-canonical requests for FQDNs or domains with appended port numbers -- or both. Ane example would be: [google.com.:80...] which is perfectly-valid, but non-canonical.

This can be handled properly by using two RewriteConds -- one being a modified version of what you already have:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)(\.(:[0-9]+)?:[0-9]+)$
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

Replace the broken pipe "¦" character in the second RewriteCond pattern with a solid pipe before use; Posting on this forum modifies the pipe characters.


2:12 pm on June 10, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 16, 2004
posts: 58
votes: 0

Hi Jim,

Thank you very much for the help on the rules!