VirtualDocumentRoot

VirtualDocumentRoot can be used to dynamically allow the creation of virtual hosts using only directories.

For example:

VirtualDocumentRoot /var/www/vhosts/%0/httpdocs

Unfortunately, this makes support for URLs beginning with "www" difficult because they map to different directories. Rather than creating a multitude of "www." directories, you can use the following mod_rewrite in your Apache configuration to redirect users to domains with the "www." prefix removed.

Apache 2.0:

# Remove "www." from domain names
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*) %{HTTP_HOST}`$1  [C]
RewriteRule ^www\.([^\_]*)\`(.*)$ http://$1/$2 [R=301,L]

Apache 2.2:

# Remove "www." from domain names
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*) %{HTTP_HOST}`$1  [C]
RewriteRule ^www\.([^\_]*)\`(.*)$ http://$1$2 [R=301,L]

Backticks ("`") are not supported characters in domain names, so they can be used as a delimiter to separate the path from the domain name.

Of course, you will also want to modify the "http" in the redirect to be "https" for SSL virtual hosts.

VirtualDocumentRoot (last edited 2008-08-06 16:59:56 by localhost)