Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i have a site "example.com" and also many other domains like "example1.com", "example2.de", etc.

I want that every Second-Level-Domain is rewritten in the following way:

example.com/domainredirect=example1.com (when you open example1.com) and example.com/domainredirect=example2.de (when you open example2.de)

So the original Second-Level-Domain should be rewritten after "example.com/domainredirect="

Thanks in advance

share|improve this question
Can you post the examples of your attempts so far? –  EightBitTony Oct 23 '12 at 22:15
add comment

1 Answer

up vote 0 down vote accepted
RewriteCond %{HTTP_HOST} ^(www\.)?example(2|3|4).(com|de|net|org)$
RewriteRule (.*) http://example.com/domainredirect=%{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:

http://www.example2.org -> http://example.com/domainredirect=www.example2.org

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://example.com/domainredirect=%{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://example.com/domainredirect=%{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: example.com/domainredirect=example.com . 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 "example.com" 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\.)?example.com –  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
add comment

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.