Set up multiple virtual hosts on XAMPP for windows

As I work on many projects and experiments on my local windows development environment (XAMPP), I normally put all web stuff in the htdocs directory and usually I’m happy with it. For several projects (like CMS installations or experiments with frameworks) it’s very handy to have a separated virtual host in order to be able to have an own document root for that application. You just don’t have to bug with different paths and subdirectories and can focus on developing instead of setting base URLs etc.

For example, if I develop MVC applications which are meant to run in a host’s document root, I want to work with paths like http://<host>/<controller>/<action> without having to deal with subdirectories like http://<host>/directory/subdirectory/public/<controller>/<action>.
It just facilitates developing and even tough it’s a little more work at setting up a new project, usually it’s worth it. Here’s a quick and dirty how to achieve this in two steps.

1. Create the virtual host entries

This how-to is based on the actual version (1.6.7) of XAMPP. As XAMPP is prepared for what we want to do, you just have to edit the following file:

X:\<path to your xampp installation>\apache\conf\extra\httpd-vhosts.conf

First of all, uncomment the following line to enable name based virtual host on your server’s port 80:

NameVirtualHost *:80

Then you can start adding your virtual hosts. The following listing is a skeleton of what I usually use. I will assume we create a project which should be accessible by entering http://testproject in your browser’s address bar.

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot D:/srv/xampp/projects/testproject/public
	ServerName testproject

	<Directory "D:/srv/xampp/projects/testproject/public">
		Options Indexes FollowSymLinks Includes ExecCGI
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>

Just make sure the DocumentRoot exists and matches the Directory and remember the value you set for ServerName.

2. Edit your windows hosts file

Now that your apache is ready to go, you have to tell your system what to do if you enter http://testproject in your browser. The most simple way without having to deal with DNS or anything else is to edit your hosts file you can find here (should be obvious that you have to alter the path if you got windows installed elsewhere):

C:\WINDOWS\system32\drivers\etc

The file is just a simple text file which contains IP-to-hostname mappings. Edit the file with a text editor and append a new line which maps the hostname you specified in apache’s ServerName-directive to 127.0.0.1. You can place it just under the existing one which defines localhost. In the end, your file could look like this:

# some comment stuff

127.0.0.1		localhost
127.0.0.1		testproject

Done

Save the file and you should be done. Remember to restart your apache in order to load the new configuration. If all went fine, you should land on your virtual host if you enter the specified hostname in your browser.

If XAMPP doesn’t start anymore or your server is not reachable, take a look at the error log and check the apache config file(s) for typos or errors.

Update: I forgot to mention, that you lose access to your default htdocs directory by following the steps above. To fix this, you have to create an extra vhost with ServerName localhost which points to your htdocs directory.