How to Create a Subdomain on WAMP

As a developer first step to build a web application is setup on local machine. Which includes setting up virtual hosts for local application address. This post will walk you through the steps to create subdomain/V-Host on Windows using WAMP.

How to Create a Subdomain on WAMP

Add an entry in hosts file

Open the hosts file and add an entry for your subdomain. On windows machine this file can be found in the following location:

c:\windows\system32\drivers\etc\ 

This file has no extension just "hosts" file. So you have to open it using notepad or any editor of your choice. It also requires Administrator permissions so open it using "Run as Administrator". Add the following lines at the end of file:

127.0.0.1  main-domain.com
127.0.0.1  sub-domain.main-domain.com

Leave the first line as it is where it says "localhost" instead add the content of above snippet to file. The first line from snippet above is for your main domain whereas second line is for your sub-domain. You can change the main-domain.com and sub-domain.main-domain.com to any domain of your choice, I am using it for this post as an example. What actually this file does? 127.0.0.1 is self referenced loop back IP. Whenever this IP is accessed it will reference the local machine to resolve IP address to a hostname . That means the operating system will search for the given hostname on the same computer for server instead of looking it up in configured DNS.

Now that you are done pointing your domain back to your local machine open the "httpd-vhosts" file on the following location for WAMP:

C:\wamp\bin\apache\Apache2.2.11\conf\extra\

I have WAMP installed in c:\

 

Add an entry for VirtualHost (Sub-domain)

After opening the file add the following lines for your main domain and sub-domain save the file and exit. Its best to keep a backup of file before making any changes.

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www"
    ServerName main-domain.com
    ServerAlias www.main-domain.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/sub-domain"
    ServerName sub-domain.main-domain.com
    ServerAlias www.sub-domain.main-domain.com
</VirtualHost>

DocumentRoot points the directory where files for given domain are stored in my case I have a directory "sub-domain" inside of root direcoty "www".

Enable VirtualHosts

Now that you have followed all the instructions properly its time to enable your VirtualHosts in httpd.conf file. The file can be found on the following location:

C:\wamp\bin\apache\Apache2.2.11\conf\

or

To open httpd.conf simply left click your WAMP Tray icon, move your cursor over apache, and choose the file httpd.conf in sub-menu.

After opening httpd.conf search for the following lines and remove the "#" from the beginning of lines then save and exit.

#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#Include conf/extra/httpd-vhosts.conf

All done! you need to restart WAMP server, open your browser and enter main-domain.com and then sub-domain.main-domain.com. You should be able to see the www as root of main-domain.com and www/sub-domain as your sub-domain.main-domain.com.