Add a virtual host in XAMPP for Windows

When using XAMPP for Windows, and working in a local development environment, you probably want to add an virtual host. If you add a virtual host, you don not have to access your site with localhost/yoursite, but you can access the site with yoursite.local instead.

This is a two step process. (inspiration: http://butlerccwebdev.net/support/testingserver/vhosts-setup-win.html). Make sure, that you have stopped Apache and MySQL under the whole process.

Edit the Windows host file

First you have to open up your text editor as administrator (if not opening the text editor as administrator, you are not able to change in Windows core files).

Then navigate to C:\Windows\System32\drivers\etc and open op the hosts file.

In the end of the file, add 127.0.0.1 localhost. For each site you want to point to, add 127.0.0.1 yoursite.local on a new line.

Your hosts file should look something similar to this:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
	
127.0.0.1      localhost
127.0.0.1      yoursite.local

Save the file, and close it.

Add the virtual hosts to the Apache config file

Navigate to C:\xampp\apache\conf\extra and open the file httpd-vhosts.conf.

In the end of the file, add following:

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    ServerAlias localhost
</VirtualHost>

and for each site you want to point to, add follwing:

<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/yoursite"
    ServerName yoursite.local
    ServerAlias yoursite.local
</VirtualHost>

Save and close the file.

Try it out

Now start Apache and MySQL, and open yoursite.local in your browser, to see if everything is working correct.

You can always add more virtual hosts later.

ms@morningtrain.dk'

Martin Schadegg Rasch Jensen