Apache and mod_rewrite: redirect domain to subdirectory

(I know this is not a programming issue per se, but it does include regular expressions, so at least it's a border ...)

Setup:

Apache 2.0 with mod_rewrite on Windows. Two domains, 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 enters "domain2.example" into his browser, I want him to go to the ** subdirectory on the server, but leave the domain that he printed intact ("domain2.example / domain2 /"), of course, the redirect should leave all absolute and relative links on the pages under this domain / directory.

Is this possible with mod_rewrite (or Apache virtual hosts or another method) and how to do it?

** The "Subdirectory" in this case is not really a file folder on the disk, but a virtual folder created using the Apache "Location" directive.

Thanks.

+5
source share
3 answers

I do not think you need to use mod_rewrite, you should use vhosts for this, as you suggest. To do this, you will have one host with the server name domain2.example, which points to the desired directory. This will also use the ServerAlias ​​directive for domain1.example, so the requests for this go to the same directory.

ServerAlias DocumentRoot. , , URL-, mod_rewrite.

+2

, Apache, , - . , , .htaccess :

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

, http://foo.yourwebsite.com/bar /path/to/your/subdomains/foo/bar

+6

, , .

- apache.

+3

All Articles