I use WAMP on my computer to simplify the process of installing and running Apache, PHP and MySQL. The three together effectively mimic the setup on my web host, allowing me to develop for the web with minimum effort. However, one thing that I had never done until now is to set up a virtual host. I had simply placed directories within the ‘www’ folder and used ‘localhost/websitename’ to view the pages in my web browser. This led to problems with using relative links, as using ‘/’ would take me to the ‘localhost’ directory, rather than the ‘websitename’ directory.

The solution (which I have now discovered is very simple) is to setup a virtual host. I decided to setup a virtual host called ‘keanei’ which would show the directory ‘www/keanei’.

Firstly, you need to edit WAMP’s httpd.conf file to allow virtual hosts. To do this, click on the WAMP icon in the system tray, then select Apache and click on httpd.conf:

WAMP - open httpd.conf for editingThe file will open in Notepad. Now, uncomment the line below ‘#Virtual hosts’ so that the virtual hosts configuration file is included:

WAMP - edit httpd.confNow you need to open httpd-vhosts.conf for editing. The default path for this is ‘c:\wamp\bin\apache\Apachex.x.xx\conf\extra’, although on my PC I have WAMP installed on d:. As you can see below, I’ve added two virtual hosts – one that maps ‘localhost’ to ‘d:\wamp\www’, and one that hosts ‘keanei’ to ‘d:\wamp\www\keanei.co.uk’:

WAMP - edit httpd-vhosts.confThe next step is to edit the Windows ‘hosts’ file. The usual path for this is ‘c:\windows\system32\drivers\etc’. The basic file will include an entry for ‘localhost’, but I’ve added one for ‘keanei’:

Edit Windows hosts fileFinally, restart WAMP, and the new configuration will be in place. Now I can type ‘keanei’ into my web browser and it will display the contents of ‘d:\wamp\www\keanei.co.uk’, and most importantly all relative links will now work correctly!

One thought on “Creating virtual hosts with WAMP

Leave a Reply

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