Blog Archive

Saturday 5 October 2013

WAMP, virtual hosts & subdomains

WAMP is my development environment of choice, and I usually have simultaneous projects in progress. Here's my solution to keeping them all separate & discrete during that process. It involves serving projects as subdomains of the local server, the advantage of this being primarily that all projects behave as if they were at the root level, meaning no special .htaccess editing per project. Transferring these projects to a staging or live server is then usually transparent.


  1. Open httpd.conf and un-comment the Virtual hosts line, if it is not already enabled. It should look something like:

    Include conf/extra/httpd-vhosts.conf

  2. Open that file up (httpd-vhosts.conf) and edit it so it is something like:

    NameVirtualHost *:80
    <VirtualHost *:80>
    ServerName localhost.dev
    ServerAlias *.localhost.dev
    VirtualDocumentRoot c:\wamp\www\subdomains\%1
    ErrorLog "logs/subdomain_errors.log"
    <directory "c:\wamp\www\subdomains\%1">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from all
    </directory>
    </VirtualHost>

    In this example I am going to keep all my projects in a sub-folder of the WAMP www directory named "subdomains". It can be named whatever you like.

    The virtual host is being set up to serve "anything.localhost.dev" from the subdomains folder where "anything" matches a folder inside of that. So "project1.localhost.dev" would actually serve files from "c:\wamp\www\subdomains\project1". If you keep your WAMP directory somewhere other than the c: drive, you will need to change the path accordingly.

  3. Next, the hosts file will need to understand that "anything.localhost.dev" refers to the loopback address, same as "localhost". The hosts file is usually found on a Windows system in "C:\Windows\System32\drivers\etc". I like to use an editor like HostsMan to do this, although notepad will do too, and you will probably need to edit the hosts file as an administrator. Add a line for each project in this fashion:

    127.0.0.1 project1.localhost.dev
    127.0.0.1 project2.localhost.dev
    ...

    Incidentally, I chose the suffix ".dev" simply because it is not a valid DNS one, and clearly indicates "development".

  4. To enable the hosts change you will probably need to flush the DNS cache. HostsMan has a menu setting to do this, although yo can do it from the command line with

    ipconfig /flushdns

  5. To enable the changes to httpd.conf and httpd-vhosts.conf you will need to restart WAMP, if you haven't already.Make sure the rewrite and vhosts modules are enabled.

Your browser should now be able to serve http://project1.localhost.dev as expected, from files in "c:\wamp\www\subdomains\project1".

No comments:

Post a Comment

My top artists