Map Apache virtualhost to a subdirectory of another virtualhost using .htaccess | UbuntuCafe

Map Apache virtualhost to a subdirectory of another virtualhost using .htaccess

This can actually be accomplished using a single <VirualHost/>, by using the ServerAlias directive. You can then use the RewriteRule to pass those requests to the proper directory, and by leaving off [R] you will just be rewriting the request, leaving the URL intact.

map apache virtualhost


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example1.com
    ServerAlias example2.com

    <Directory /var/www/example1>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order deny,allow
        Allow from all
    </Directory>

    DocumentRoot /var/www/example1

    RewriteEngine On
    RewriteCond %{HTTP_HOST} example2\.com [NC]
    RewriteRule (.*) /example2$1 [L]

</VirtualHost>

Or remove highlighted code from above VirtualHost and add the below code to .htaccess of example1's root directory.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} example2\.com [NC]
    RewriteRule (.*) /example2$1 [L]

Post a Comment