Adding sub domains to localhost

I want to separate each of my project and like to have separate domain name for that. This is  for various reasons like I don’t want relative paths. I want to replicate production environment as much as possible. This way one can avoid unexpected errors that when moving site to live. Follow below steps if you want to achieve the same.

Step 1

Add domain name to hosts file. I am on ubuntu environment so my hosts file’s path is

/etc/hosts

if you are on other environment find out path to hosts file and edit as administrator.

Step 2

Now add domain names to file like below

127.0.1.1 subdomain.localhost
127.0.1.1 domainname1
127.0.1.1 domainname2

Step 3

Now you have to add virtualhost entry in apache config file. If you are using xampp there is separate config file to edit virtual hosts. If you dont know which one is that , just add below code at the bottom of main config file httpd.conf in my case.

<VirtualHost *>
ServerName localhost
DocumentRoot /opt/htdocs/
</VirtualHost>

<VirtualHost *>
ServerName subdomain.localhost
DocumentRoot /absolute/path/to/folder/
</VirtualHost>

<VirtualHost *>
ServerName domainname1
DocumentRoot /absolute/path/to/folder/
</VirtualHost>

<VirtualHost *>
ServerName domainname2
DocumentRoot /absolute/path/to/folder/
</VirtualHost>

Restart apache to see the changes.