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 with different sections. There are several domains pointing to the same site. What I'm trying to achieve is.

Say if my site has the domains, and

If the user comes from the user should get it should not be redirected, it should be rewritten like this :

  1. should become
  2. should become

Same way if the user is coming from the content served should be and the domain rewritten like

  1. should become
  2. should become
share|improve this question

This seems like a case for mod_rewrite.

Something like this ought to do the trick:

RewriteEngine on
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^([^.]+\.com)(/.*) $1$2  [P]

You will need to enable both mod_rewrite and mod_proxy (a2enmod rewrite && a2enmod proxy) for this to work.

It's worth noting that if the request is for this will not work. If you want to account for subdomains (and strip them), you should use this for the second RewriteRule instead:

RewriteRule   ^([^/]*\.)?([^.]+\.com)(/.*) $2$3  [P]
share|improve this answer
does this do to – Safwan Erooth Oct 8 '10 at 17:20
The first rule adds the requested host to the query string and the [C] modifier chains it to the next rule. The second rule uses the [P] modifier to do an internal proxy which should be transparent to the requester. So the user will request and will get back the content of thinking that they got it from Is that what you mean? – mark Oct 8 '10 at 17:31
yes.. thats what i mean – Safwan Erooth Oct 10 '10 at 5:01
If he knows the directory where the files are located then he won't need mod_proxy (which is almost always disabled on shared hosting sites). He could use a rule like RewriteRule ^([^.]+\.com)(/.*) /path/to/site/$1$2 then. – Chris S Jan 12 '11 at 13:50

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.