Most web developers and template creators need to have multiple sites on their local computer that can be accessed any time without modifying apache configuration variables. The easy solution is each site to be in a www root folder sub-directory and access that site like that: http://www.mysite.org/siteA/. Here we will show you how you can easily create virtual hosts on a windows machine to run multiple sites simultaneous each one having its own domain (http://siteA.tld).  In this guide I will show you the general method of creating virtual hosts that can  be applied to XAMPP & Zend Server both.

Locating needed files

For this guide we need to edit 3 files:

1. The Apache configuration file (httpd.conf).
This file is located inside the Apache’s installation directory. Example path from my local computer:

C:/Program Files/Zend/Apache2/conf/httpd.conf

The above path may vary depending on the location of the windows installation drive on your computer and the apache version.
If you use XAMPP this file should be located inside the XAMPP installation directory. If you cannot find it use search.

2. The Apache Virtual hosts configuration file (httpd-vhosts.conf)
This file is located inside the Apache’s installation directory. Example path from my local computer:

C:/Program Files/Zend/Apache2/conf/extra/httpd-vhosts.conf

The above path as well as the configuration file name may vary depending on the location of the windows installation drive on your computer and the apache version.
I think XAMPP by default adds virtual hosts inside the httpd.conf file. Sorry, I have never used XAMPP personally as I used Zend Server (Community Edition)

3. Windows hosts file (hosts)
This is a file with no extension hidden inside the windows/system32/drivers/etc/ folder. It contains a list of the system hosts (normally only localhost). Example path for my local computer:

C:/windows/system32/drivers/etc/hosts

Notice: This guide is for Windows Vista/Windows 7 users

Examine your www root folder

Foreach one of you sub-folders/sites inside your www root folder we will create a virtual host. Go to your www root folder where you have your local sites. This by default is:
C:/Program Files/Zend/Apache2/htdocs

Or a htdocs folder inside the XAMPP installation directory or any custom path you have set in httpd.conf as DocumentRoot.

In this folder you will have a directory structure like this:
siteA (folder where site A is located in)
siteB (folder where site B is located in)
etc….

For consistency we will use these folder names as domain names.

Add virtual hosts to Apache

Open Apache’s Virtual hosts configuration file (httpd-vhosts.conf) with a text editor such as notepad, editpad, pspad etc. Make sure everything is commented by adding a sharp symbol (#) in front of each line. Everything pre-existing there are just samples. First of all write:

NameVirtualHost *:80

 <VirtualHost *:80>
     DocumentRoot "F:/myweb"
     ServerName localhost
 </VirtualHost>

Where you should replace the F:/myweb (my local DocumentRoot) with the path of your WWW root folder (normally camed as htdocs).

Now, for each one of your sites (the sub-directories we talked about previously) create an entry like this:

<VirtualHost *:80>
     DocumentRoot "F:/myweb/site1"
     ServerName sitename1.dev
 </VirtualHost>

When finish your httpd-vhosts.conf file should look like this:

NameVirtualHost *:80

 <VirtualHost *:80>
     DocumentRoot "F:/myweb"
     ServerName localhost
 </VirtualHost>

 <VirtualHost *:80>
     DocumentRoot "F:/myweb/site1"
     ServerName sitename1.dev
 </VirtualHost>

 <VirtualHost *:80>
     DocumentRoot "F:/myweb/site2"
     ServerName sitename2.dev
 </VirtualHost>

 <VirtualHost *:80>
     DocumentRoot "F:/myweb/site3"
     ServerName sitename3.dev
 </VirtualHost>

etc…

Notice that for consistency we used folder names as domain names, you can change this if you wish. We also used the loc as a pseudo-TLD *. You can use anything you wish but I advise you not to use a real one (like com, net, org, gr, fr, etc).

* TLD: Top Level Domain

Prepare Apache to handle virtual hosts

Now that my virtual hosts file is ready I must tell apache to use that file. Open Apache’s configuration file (httpd.conf) with a text editor. Locate the line saying about where Virtual hosts file is located in (somewhere in the bottom of the document). If this line does not exist add it!

# Virtual hosts
# Include conf/extra/httpd-vhosts.conf
Uncomment the second line in order to tell Apache to include our virtual hosts file:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Add virtual hosts to Windows

Open Windows’s hosts file (hosts) with a text editor. Inside it you will find a line like this:

127.0.0.1    localhost

Add bellow this line:

127.0.0.1    dev

Finally add all the virtual hosts you created at Apache:

127.0.0.1   sitename1.dev
127.0.0.1   sitename2.dev
127.0.0.1   sitename3.dev

etc…

Your Widnows hosts file is ready. Save it. Restart Apache. If needed restart Windows.
Now you can access all of your local sites like this:

http://sitename1.dev

http://sitename2.dev

http://sitename3.dev

No more sub-directories and ugly urls :-)