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

I have a site mysite.com 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 mysite.com, section1.com and section2.com.

If the user comes from section1.com the user should get mysite.com/section1/ it should not be redirected, it should be rewritten like this :

  1. mysite.com/section1/something should become section1.com/something
  2. mysite.com/section1/another should become section1.com/another

Same way if the user is coming from section2.com the content served should be mysite.com/section2/ and the domain rewritten like

  1. mysite.com/section2/something should become section2.com/something
  2. mysite.com/section2/another should become section2.com/another
share|improve this question

1 Answer 1

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)(/.*)           http://mysite.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 http://www.section2.com/foo 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)(/.*)           http://mysite.com/$2$3  [P]
share|improve this answer
does this do mysite.com/section2/something to section2.com/something –  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 section2.com/foobar and will get back the content of mysite.com/section2/foobar thinking that they got it from section2.com/foobar. 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.