5

(I know this is not a programming question per se, but it involves regular expressions, so at least it is borderline... )

The setup:

Apache 2.0 with mod_rewrite on Windows. Two domains, let's call them domain1.example and domain2.example. I would like to host both domains on the same server ("server1"), so I point them to the same IP address.

Now, if the user types "domain2.example" into his browser, I want him to end up in a subdirectory** on the server, but leave the domain he typed intact ("domain2.example/domain2/"). The redirection must leave all absolute and relative links on pages under this domain/directory intact, of course.

Is this possible with mod_rewrite (or Apache virtual hosts or other method), and how do I do it?

** The "subdirectory" in this case is not actually a file folder on disk, but a virtual folder made with the Apache "Location" directive.

Thanks.

3 Answers 3

7

I'm assuming you don't have access to the Apache configuration, otherwise, yes, virtual hosts are your best option. If you don't, however, this can be put in a .htaccess file and should do the trick:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.yourwebsite\.com$
RewriteRule ^/(.*)$ /path/to/your/subdomains/%1/$1 [L]

So a request to http://foo.yourwebsite.com/bar would go to /path/to/your/subdomains/foo/bar

3

Assuming that the domains are independend the advised solution is virtual hosts.

You can find the documentation at the apache website.

3
  • Will the DocumentRoot directive (in the vhost config section) work to forward to a "subdirectory" that's really a "Location" and not a physical file folder on the server? Apr 22, 2009 at 6:25
  • What do you mean with Location and not file folder? I guess you have an index.html/index.php/index.cgi somewhere and that you put the DocumentRoot to the folder where that file is located in.
    – Peter Smit
    Apr 22, 2009 at 6:32
  • With "Location" I mean this: httpd.apache.org/docs/2.0/mod/core.html#location Specifically combined with the SetHandler directive: httpd.apache.org/docs/2.0/mod/core.html#sethandler So there is no physical file (index.htm, index.php, whatever), but rather a forwarding to a "Handler" (pls_handler, ie. mod_plsql, in my specific case). Apr 22, 2009 at 10:03
2

I don't believe that you need to use mod_rewrite, you should be able to use vhosts for this like you suggest. To do this, you will have a single vhost with servername domain2.example which points to the directory you want. This will also use the ServerAlias directive for domain1.example so requests for this go to the same directory.

See the documentation for ServerAlias and DocumentRoot. Also note that if you want the directory to show up in the URL, you will need to use mod_rewrite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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