Apache virtual hosts config

This approach uses the vhost_alias module combined with a local DNS to let you use any subfolder in your web directory as a virtual host without having to do any additional Apache configuration. This description is for Apache on Windows; the same Apache config will work on any system, but you’ll need to find and configure a local DNS service for your OS to get the full benefit.

1. Apache configuration

First, make sure mod_vhost_alias.so is enabled in httpd.conf.

LoadModule vhost_alias_module modules/mod_vhost_alias.so

Next, place the following code in your httpd-vhosts.conf file:

 ServerName normal.dev
 ServerAlias *.dev
 DirectoryIndex index.php index.html
 UseCanonicalName Off
 VirtualDocumentRoot D:\Jeremy\htdocs\%1\

If you now create a directory like so:

--- htdocs
  |
  --- testsite
    |
    --- index.html

This configuration will allow you to visit http://testsite.dev/index.html without having to add a new vhost in your apache config.

2. DNS configuration

However, before you can test this in your browser, you first need to map testsite.dev to localhost so that your browser can get to your Apache instance and pick up on that nice autoconfigured virtual host. There are two ways you can do this on Windows. One method is to edit the hosts file (type notepad c:\windows\system32\drivers\etc\hosts into the Run dialog) and add a line for each virtual host you want to use:

127.0.0.1        testsite.dev
127.0.0.1        testsite2.dev

Unfortunately, the windows hosts file doesn’t support the use of wildcards, so you can’t just define *.dev and be done with it. So, the preferred method is to install a DNS service like Acrylic. (Download page is here.) Once you’ve done that, you can add wildcards to the Acrylic custom hosts file, which looks just like the windows hosts file:

127.0.0.1        *.dev

3. Configuration for Symfony2

But what if your project, like Symfony2, requires a webroot within the project directory itself? Consider the following configuration:

--- htdocs
  |
  --- testsite
    |
    --- app
    |
    --- bin
    |
    --- src
    |
    --- vendor
    |
    --- web

You don’t want to have all your code in the web root, but you don’t want to have to store all your project files in htdocs either. So you need to define the VirtualDocumentRoot to point to a subdirectory of your project directory.

 ServerName symfony.sym
 ServerAlias *.sym
 DirectoryIndex index.php index.html
 UseCanonicalName Off
 VirtualDocumentRoot D:\Jeremy\htdocs\%1\web

Now, if you visit testsite.dev, Apache will set the document root to /testsite, according to the first set of rules we created; if you visit testsite.sym, Apache will look for a Symfony2-appropriate document root in /testsite/web. This lets you develop projects with various different directory structures without having to change your Apache config.

Don’t forget to create the appropriate rule in your Acrylic configuration too:

127.0.0.1        *.sym

Leave a Reply

Your email address will not be published. Required fields are marked *