Apache VirtualDocumentRoot Configuration
apache virtualhost server configuration web linux
Using Apache's VirtualDocumentRoot to dynamically route domains and subdomains to specific directory paths
It uses Apache’s VirtualDocumentRoot to direct domains down a specific directory path based on the URL that is making the request to the server.
<VirtualHost *:80>
UseCanonicalName Off
VirtualDocumentRoot /var/www/virtual/%-2.0.%-1/%-3
ErrorLog logs/error_log
CustomLog logs/access_log common
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.[a-z0-9]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
If the domain making the request to the server is “jasonbrennan.com” it will be rewritten to “www.jasonbrennan.com” and look down this directory path for the index files.
/var/www/virtual/jasonbrennan.com/www/
If the requested URL is “dev.jasonbrennan.com” the path will change as follows.
/var/www/virtual/jasonbrennan.com/dev/
If you are using Mod_rewrite don’t forget to add the following to a “.htaccess” file in your document root.
RewriteBase /