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: 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 localhost. For each site you want to point to, add 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:
#          # source server
#              # x client host

# localhost name resolution is handled within DNS itself.
#       localhost
#	::1             localhost      localhost      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

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

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

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.'

Martin Schadegg Rasch Jensen