Setting Up Virtual Hosts with XAMPP and Windows 7

Even though I’ve been a web developer for a long time now, it has always been as a full-time employee of one mega-site (such as  I’ve never had to set up my development environment to accomodate many sites until now.  I’ve been doing some contract work and it only made sense to set each site up as a virtual host, and today I figured out how to do that with XAMPP on Windows 7.
First let me clairify what I mean by virtual hosts.  Let’s say you have a client called Soup for Cats, Inc.  And you store that client’s website code in C:\Development\SoupForCats\.  A virtual host would make it so when you type http://soupforcats.local into your web browser, it does not go out into the Internet but instead loads the files stored in the directory you specify.  There are two main steps to this.
1. Edit C:Windows\system32\drivers\etc\hosts as an Administrator. To do this, go in the Start menu and right-click on Notepad.exe or any other text editor and “Run as Administrator” then navigate to the file and open.  It should be empty except for comments.  To the bottom of this add one line for each virtual host you would like:      soupforcats.local      soupfordogs.local
This tells your computer that requests to these addresses need to be routed back to your localhost server instead of sent out into the Internet.
2. To get a little more specific about what your localhost should do with these requests, we need to configure your XAMPP server’s virtual hosts file, found in C:\xampp\apache\conf\extra\httpd-vhosts.conf.  This should also be pretty empty except for comments.  Here is what you should put after the comments to set up soupforcats.local:

NameVirtualHost *
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
<VirtualHost *>
DocumentRoot "C:\Development\soupforcats"
ServerName soupforcats.local
ServerAlias www.soupforcats.local
ErrorLog "logs/soupforcats-host2.localhost-error.log"
CustomLog "logs/soupforcats-host2.localhost-access.log" combined
<Directory "C:\Development\soupforcats">
Order allow,deny
Allow from all
AllowOverride All
Options Indexes FollowSymLinks

That first line and first VirtualHost is so your standard localhost will continue working and routing you to XAMPP settings in case you need that.  After that, you are telling your XAMPP server to use the directory C:\Development\soupforcats for any requests to soupforcats.local.  The alias is just in case any of your links end up looking like www.soupforcats.local, those will work fine also now.  Logs are always good in case you need to troubleshoot something.  The last bit sets up the permissions for what the code in that directory is allowed to do.  The site I was working on was a mess until I figured out that i needed ‘AllowOverride All’ which allows the .htaccess file in your client’s directory to actually work, which is essential in most content management systems that route everything through index.php.

Leave a Reply

Your email address will not be published. Required fields are marked *