A few months ago I had presented one way of automatically assigning subdomains on a local testing web server, without having to edit your httpf.conf file all the time. For those who hadn't been following this blog, I'm talking about my “Holy Grail of local web development servers” article, achieving subdomain names in the format myapp.local.web by simply creating the folder myapp on your local web server's root. Even though the solution presented last time was elegant, it lacked that supernatural touch of a really great solution. I could never quite stomach those ugly URL rewriting rules. So, here it is: we revisit this issue and improve the solution!

Whatever you do, you still have to setup BIND and create a local DNS zone for the solution to work at all. But configuring Apache may be just a bit easier. In my examples I'm going to use WAMPServer, but the same holds true for just about any other pre-packaged Apache distribution, or a roll-your-own web server installation on any operating system.

One of the lesser known features of Apache - at least, lesser known to those who are not ISPs and host farms - is mod_vhost_alias. This extremely simple Apache module takes care of mass virtual hosting, i.e. assigning and dynamically configuring virtual hosts based on the IP or host name appearing in the request coming from the browser. In order for us to create subdomains, we normally have to create virtual host entries in our httpd.conf. What we're trying to achieve is assign each subdirectory on our web server root to a same-named subdomain. Sounds familiar? It's exactly what mod_vhost_alias does.

Before going on to configure it, I wanted to make my solution sustainable. I mean, WAMPServer has spoiled me, allowing to run different Apache/PHP/MySQL version combinations, switching the version of each used component on the fly. I have ended up with four Apache versions sitting on my hard disk, each one with its own httpd.conf file, ready to be swapped at the press of a button. I wouldn't like having to copy the same configuration around. Thankfully, the last line of each Apache version's httpd.conf file in WAMPServer includes all .conf files in c:wampalias. As a result, my solution would fit inside a single file: c:wampaliasdynamic_vhost.conf.If you don't run WAMPServer you can simply append the following code to your httpd.conf file.

Here is the configuration code you need to use:

<IfModule !mod_vhost_alias.c>
 LoadModule vhost_alias_module modules/mod_vhost_alias.so
NameVirtualHost *:80
<VirtualHost *:80>
 ServerAdmin This email address is being protected from spambots. You need JavaScript enabled to view it.
 DocumentRoot "C:/wamp/www"
 ServerName www.local.web
 ErrorLog "c:/wamp/logs/error_master_log.log"
 CustomLog "c:/wamp/logs/access_master_log.log" common
<VirtualHost *:80>
 ServerAlias *
 UseCanonicalName Off
 VirtualDocumentRoot "C:wampwww%1"

A few notes. I have used c:\wamp\www as the base of all my paths. You have to replace that with the absolute path to your own server's root. Moreover, I assume that the local DNS zone you have created in BIND is local.web and that you want to access the web server's root as www.local.web. If this is not true, you can modify the ServerName line in the file.

That was all! Really. All it takes to massively configure hundreds or thousands of subdomains is a 17-line configuration file.

If you have any questions or comments I'd love to hear your feedback! You can either leave your comment below, or use the contact form to send me an email.

No thoughts on “The Holy Grail of local web servers revisited”