How to Create a Subdomain on WAMP Server (Step-by-Step Guide)

As a web application developer, the first step towards developing any web application is to set up an environment for development on a local machine. This involves setting up virtual hosts for local application addresses. This post will walk through the steps to set up WAMP Virtual Hosts on Windows.

How to Create a Subdomain on WAMP

To configure a local subdomain on WAMP, we must first enable virtual hosts and the virtual host alias module in WAMP. Learn in a few simple steps how to create WAMP Virtual Hosts to set up a local subdomain.

  1. Add an entry to the Windows host file to look for a domain on the local machine.
  2. Enable Virtual Hosts and Virtual Hosts Alias Module in WAMP Apache.
  3. Add entries to the Virtual Hosts file for our desired domains.
  4. Test the subdomain implementation.
 

What is a Subdomain/Virtual Host?

A subdomain in WAMP is a locally configured virtual host that can be accessed with a unique URL instead of accessing the directories inside a project. This feature allows developers to run multiple projects by mapping custom addresses to different directories. The following are the steps to configure a WAMP subdomain on Windows.

 

Step 1: Edit The Hosts File to Create a Subdomain on WAMP

First, add an entry for subdomain in the hosts file. On a Windows machine, this file can be found at the following location:

c:\windows\system32\drivers\etc\

This file has no extension, but is just a "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 the 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", and add the content from the above snippet to the file. The first line from the snippet above is for your main domain, whereas the second line is for your subdomain. 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 does this file actually do? 127.0.0.1 is a self-referenced loop back IP. Whenever this IP is accessed, it will reference the local machine to resolve the IP address to a hostname. That means the operating system will search for the given hostname on the same computer for the server instead of looking it up in the configured DNS.

 

Step 2: Enable Apache Virtual Hosts in WAMP

After adding an entry to Windows' hosts file, it is time to enable virtual hosts and the virtual host alias module in httpd.conf file. The file can be found at the following location:

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

To open httpd.conf, simply click the WAMP Tray icon, move the mouse cursor over Apache, and click httpd.conf in the sub-menu.

After opening httpd.conf, search for the following lines and remove the # from the beginning of these lines, then save and close the file.

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

Now that we are done pointing our domain back to the local machine, open the httpd-vhosts file at the following location:

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

I have WAMP installed in C:\ directory.

 

Step 3: Configure Apache VirtualHost for WAMP Subdomain

After opening the file, add entries for virtual hosts with the following lines for our main domain and subdomain, then save and close the file. It is best to keep a backup of the original 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 sub-domain.main-domain.com
</VirtualHost>

The DocumentRoot is the directory where files for the given domain are stored, in my case. I have a directory sub-domain inside the www root directory. The ServerName is the domain name or subdomain name. The ServerAlias is an alternate domain, which is usually www. We did not use any for our subdomain in the second entry above.


Step 4: Restart WAMP Server and Test Local Subdomain Configuration

After following the above steps, we need to restart the WAMP server, open a browser, and enter main-domain.com and then sub-domain.main-domain.com to confirm our configuration. We should see the content of www as the root of main-domain.com and the content of www/sub-domain for our sub-domain.main-domain.com as per our configurations.