Jure Merhar

Mapping subdomains to subdirectories with Apache – the easy way

For some time now, I have been mapping projects to subdomains on my development server. In practice, this means that I access my websites thorugh project1.mydomain.com, project2.mydomain.com, etc. This way, I only need one domain name for all my projects in development, and still have the benefit of having each project reside on a separate (sub)domain, which simplifies the use of SEF URLs.

One way to map each project to it’s own subdomain is by creating one VirtualHost for each project. Although this process can be automated to some extent, it is still tiresome having to do this every time you start a new project. In addition, you quickly end up with a lot of copies of basically the same configuration file, the only difference being the subdomain name and the directory. Clearly, this is not a very DRY approach.

To achieve this mapping automatically – without the need for multiple virtual hosts – I have been using mod_rewrite in the web root directory to map subdomains to subdirectories. So, for the example above, I would have two subdirectories – project1 and project2 – each containing one of the websites I’m currently working on. The script responsible for making this work, was an .htaccess file in the web root (parent) directory, the contents of which looked something like this:

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.mydomain\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

I found this code in a webmasterworld.com forum post, after devising something similar myself. While this solution works fine in most cases, it does have some drawbacks. The one that bothered me most is that the DOCUMENT_ROOT environment variable is set to the web root and not the project root directory. This can cause all sorts of problems for scripts relying on it.

Looking for a way to circumvent this problem, I inadvertently stumbled upon this page: Dynamically configured mass virtual hosting and was amazed at how easily this subdomain to subfolder mapping can be done with Apache. All you have to do, is replace the DocumentRoot line in the Apache config file with this:

VirtualDocumentRoot /var/www/%1/

What this does, is it takes the first part of the requested host header (everything up to the first period) and sets the DocumentRoot accordingly. So, project1.mydomain.com now points to /var/www/project1/, project2.mydomain.com to /var/www/project2/, etc. For this to work though, you will need mod_vhost_alias enabled in your Apache configuration, however that should not be a problem.

And voilà, there you have it. No more need for weird rewrite rules, while at the same time fixing the DOCUMENT_ROOT environment variable problem.

In the end, I would just like to point out that I have done quite some googling on the subject when I was first implementing this, and none of the blog/forum posts I came accross suggested a solution like this. I may have been using the wrong keywords in my searches, but I am more under the impression that people just don’t know about this feature. In any case, I hope that this post helps someone who’s looking for answers on the subject.


15