rewrite rule for subdomains only

I recently encountered a situation where I wanted a rewrite rule for isapi rewrite that would rewrite all subdomain urls, but leave the main www domain alone.

So for example:
would be rewritten to
would be rewritten to

But won't be rewritten at all.

After many desk-head based collisions I finally got it working: here's the rewrite rule you need to match subdomains as the hosts you are interested in, and not www:

RewriteCond Host: ^((?:(?!www).)*)\.domain\.com

Here's the explanation of how it works:

^ # Start at the beginning of the string
( # Capture
(?: # Complex expression:
(?!www) # make sure we're not at the beginning of www
. # accept any character
)* # any number of times
) # End capture
\.domain\.com # and ending at


Posted on Sunday, July 24, 2011 1:25:00 AM GMT by Paul T.
Thanks! I was close but you had just what I was looking for!

Post a comment