Just an off the beaten track question today. Could it be posible to alter DocumentRoot of the virtualhost in line with the subdomain asked for like so?

<VirtualHost *>
   ServerName ^VARIABLE$.example.com
   DocumentRoot ~/Sites/^VARIABLE$
</VirtualHost>

Yes it's possible:


Step1: Establishing Wildcard DNS

You need to add an A Record that indicates your server's IP like this:

*.example.com.    IN      A       192.168.1.1

Step2: Setup apache VirtualHost

<VirtualHost *>
        ServerName  www.example.com
        ServerAlias *.example.com

        DirectoryIndex index.html 
        DocumentRoot /home/www/www.example.com/htdocs
    ....
</VirtualHost>

Spot the important line: ServerAlias *.example.com. This can tell Apache that any host using the .example.com suffix will match this virtual host too.

Step3: Establishing Rewrite Rules

You need to add this lines inside your .htaccess file situated inside your web root folder (eg. /home/www/www.example.com/htdocs):

RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.example.com [NC]
RewriteCond %{http_host} ^([^.]+)\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/ [R=301,L,QSA] 

This way a request foo.example.com will redirect site visitors to example.com/foo and so forth. Best of luck.


(Reference: http://www.debian-administration.org/articles/358)