Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently. This is often found on shared hosting servers.

Developers often have loads of web sites under www or htdocs folder which are then accessed on daily basis. To be able to use absolute paths in your applications, some people change the documentroot variable in httpd.conf to current working directory. This allows them to access this page with just http://localhost.

But, it isn’t efficient because you have to change the variable manually, restart Apache, and it just takes to much time.

We can use Virtual hosts on Windows to deal with this problem. As I am using WAMP server for development, this tutorial will explain how to do it in WAMP, but other products are very similar, so you want have problems porting this.

First lets edit the windows hosts file located in C:\Windows\system32\drivers\etc\hosts. Open up the file in your favorite text editor and add a line like this:

[code lang=”html”]
127.0.0.1 mydomain.home
[/code]

Basically, this tells Windows to resolve IP 127.0.0.1 (which is localhost) to mydomain.home. Save the file and close it.

Next we want to edit our httpd.conf file and httpd-vhosts.conf file. So first browse to C:\wamp\bin\apache\apache2.2.11\conf and open up httpd.conf in your favorite text editor. Then scroll down to the bottom. Look for this line:

[code lang=”html”]
#Include conf/extra/httpd-vhosts.conf
[/code]

Uncomment it (remove the # symbol in the beginning of the line). Save the file and exit. Now browse just one more folder down the tree to C:\wamp\bin\apache\apache2.2.11\conf\extras and open the httpd-vhosts.conf in your favorite text editor. Now scroll to the bottom and add this code:

[code lang=”html”]

ServerAdmin webmaster@localhost
DocumentRoot “c:/wamp/www”
ServerName localhost
ErrorLog “logs/localhost-error.log”
CustomLog “logs/localhost-access.log” common


DocumentRoot “c:/wamp/www/your-local-folder”
ServerName mydomain.home

Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1


[/code]

There are two directives for Virtual Host. First one is preserving our localhost, so you can still access the WAMP dashboard. The second one is actually telling Apache where to look when someone requests mydomain.home . Rest of the entry is allowing you to use .htaccess on this domain.

Restart Apache server.

Now, you should be able to use http://mydomain.home to develop and test your site. Whenever you need to add new site just add new entry in Windows hosts file and copy last Virtual host entry in httpd-vhosts.conf and change the domain and path.

Now you know how to add Virtual Hosts under WAMP.