Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


How can you redirect all non-active subdomains to 'www'?

Here I try to redirect all subdomains not listed to 'www'.. I can't make it work properly because %1 and %2 doesn't return anything.. Here I'm trying to redirect to just to check the returned %2 value.., but it doesn't return anything and the redirect url is

RewriteCond %{HTTP_HOST}        !^(admin|demo|www)\.([^\.]+\.[^\.]+)$ [NC]
RewriteRule ^(.*)$ [L,R=301]

this is what I really want to do

RewriteCond %{HTTP_HOST}        !^(admin|demo|www)\.([^\.]+\.[^\.]+)$ [NC]
RewriteRule ^(.*)$          http://www.%2/$1 [L,R=301]
share|improve this question
give actual examples of start end points and I can write one – Mike May 25 '11 at 15:05
like this? .... – clarkk May 26 '11 at 17:27
could anybody help? – clarkk May 29 '11 at 18:19
RewriteCond %{HTTP_HOST} !^(www|admin|files|mysql\.)domain\.com$ [NC]
RewriteRule ^{REQUEST_URI} [NE,L,R=301]

Untested but it should work..

share|improve this answer
shouldn't the \. in the rewritecond be outside the parens? – DTest May 25 '11 at 14:44
what does the NE flag mean? – clarkk May 25 '11 at 14:50
I have edited my q – clarkk May 25 '11 at 14:54
NE is no escape do you don't double encode the url – Mike May 25 '11 at 15:04

Presuming DNS entries already exist, you could use something I do on a couple domains:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
    RewriteRule ^/(.*)$$1 [L,R]

This takes requests in the form of and pushes it to

You could remove the %1 and get the following which would not use the subdomain-to-path method I'm employing thusly:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$
    RewriteRule ^/(.*)$$1 [L,R]
share|improve this answer
thats not what im looking for – clarkk May 25 '11 at 16:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.