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

i have a site "" and also many other domains like "", "", etc.

I want that every Second-Level-Domain is rewritten in the following way: (when you open and (when you open

So the original Second-Level-Domain should be rewritten after ""

Thanks in advance

share|improve this question
Can you post the examples of your attempts so far? – EightBitTony Oct 23 '12 at 22:15
up vote 0 down vote accepted
RewriteCond %{HTTP_HOST} ^(www\.)?example(2|3|4).(com|de|net|org)$
RewriteRule (.*){HTTP_HOST} [R=301,L]

What this rule above will do is it will check a condition for everything inside the () if they match it will redirect them to the domain you specified.

An example will be if a request comes into the web server for: ->

The rule is a somewhat flexible as it will work with both www. and base domain.

Hope that helps. I haven't tested it and wrote it from head but I'm pretty sure I fixed all syntax problems.

To EXCLUDE a domain from the rewrite just do the following:

To exclude from the rewrite rule just remove the (com) part, or you can add an additional condition like this...

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteCond %{HTTP_HOST} ^(www\.)?example(2|3|4).(com|de|net|org)$
RewriteRule (.*){HTTP_HOST} [R=301,L]

That should do the trick... sorry for the very delayed reply my PTO was good!

share|improve this answer
Great @Will H ! I'm almost there. My syntax is now the following: RewriteCond %{HTTP_HOST} ^(www\.)?([a-zA-Z0-9-]+).(com|de|net|org)$ RewriteRule (.*){HTTP_HOST} [R=301,L] I've changed "example(2|3|4)" because I have many other domains and the rule must match them all. But now the problem is, it redirects in this way: . And i get this error message: ERR_TOO_MANY_REDIRECTS . I think i created a loop now. Can you tell me how to except the domain "" from the rule? – Acryl Oct 24 '12 at 19:18
Updated my original answer to add the additional rewrite condition to exclude a specific host. That way the rule will check first to make sure it doesn't rewrite the base domain, if it's not the base domain then rewrite... base domain being (www\.)? – Will H Nov 4 '12 at 4:50
Thank you very much Will, it's working fine now with your solution! – Acryl Nov 4 '12 at 21:40

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.