Skip to Content Skip to Menu

Apache : Setting up sub domains on localhost

Intro

Recently I've been trying to setup subdomains to run on my localhost. This will come in handy for testing html code before putting a site live. Note I'm running Apache 2.2.11 on Windows XP. The example of a subdomain is http://localhost/mysite accessed as http://mysite.localhost

httpd.conf

At the bottom of your httpd.config (for the Apache server) usually found at c:\wamp\bin\apache\apache-<version>\conf use the following lines:

# Pretty important line - got mine working
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot C:/wamp/www/mysite
ServerName mysite.localhost
</VirtualHost>

<VirtualHost *:80>
DocumentRoot C:/wamp/www
ServerName localhost
</VirtualHost>

hosts

Running on windows you will need to change the hosts file to reference the subdomain on the correct IP address. This can usually be found at c:\windows\system32\drivers\etc. Example lines here:

127.0.0.1 localhost
127.0.0.1 mysite.localhost

Hopefully this helps someone as it took a good couple of hours of searching before I found the right solution for my application.