Setting up XAMPP on your computer in fairly easy thanks to the quick installer & control panel that enables you to set Apache & MySQL as services.

Virtual hosts allow you to set up websites on your localhost accessable using full domain names instead of via localhost/site. While they are extremely useful they can be incredibility difficult to set up if you aren’t used to working with apache.

Why would I need One?

A virtual host can be helpful for setting up a new local projects. The main reason for using one is that it will help keep your sites organized & your URL’s will be nice and neat.

So How Do I Do It?

This guide is for XAMPP since that is the local server I have, but I’m fairly confident it will work for most Apache based Windows local servers.

First create a new folder for your new site/project inside your XAMPP’s htdocs folder. This will be where all your files go, as normal. Next you need to go to your apache folder. This is generally in the xampp folder along with your htdocs folder. Then into conf, and then extra. The full path will look something like D:\xampp\apache\conf\extra.

Next open the file called httpd-vhosts.conf in a text editor. Notepad or Notepad++ are generally the best. Inside the file there will be a lot of lines pre-ceeded by pounds/hashes (#). After then add this:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "D:\xampp\htdocs\folder-name"
    ServerName domain.lh
    ServerAlias domain.lh
</VirtualHost>

You just need to change the email (if you are using that), the root to the path to the folder you created earlier, and the server name & alias to the domain name you want to use. Remember that if you use a real, existing domain name you will no longer be able to visit that site once you are finished. That’s why I use a unique domain name & a tld that (as far as I’m aware) is very uncommon, or doesn’t exist.

One more thing you need to do is enter another rule for your localhost. If you don’t you won’t be able to access your standard localhost any more, at least that’s what happened to me. So add this to your httpd-vhosts.conf file too.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "D:\xampp\htdocs"
    ServerName localhost
    ServerAlias localhost
</VirtualHost>

Now that you’ve done that there are just two steps left. First go to C:\Windows\system32\drivers\etc and open hosts in a text editor, again Notepad or Notepad++ are the best. At the very bottom add:

127.0.0.1    domain.lh

Where domain.lh is whatever you entered for the domain name & alias above. For each virtual host you add you will need to add another line to your hosts file. If you don’t Windows will not be able to route you to your local site & it will try to use your ISP’s DNS mapping system which will, of course, no nothing about the site.

The final step is to restart apache. So either use the control panel or run the xampp_restart file in the root of xampp. Once that is all finished you just need to try to access your site in a browser, if it loads great, if not check out this troubleshooting tip.

Troubleshooting

If you haven’t been able to access you local site, you can try this tip to help. Make sure apache has actually restarted. Sometimes it seems to restart, but hasn’t. Flush your DNS cache using:

ipconfig /flushdns

at a windows command prompt. I can’t confirm that this helps, but I’ve read reports of it working for others. Finally make sure you have no spelling mistakes in your Virtual Host blocks. If you have it will NOT work.

I wrote this tutorial after I received a few requests asking how to set up virtual hosts in XAMPP, so I hope it helps answer that question. As always if you have any further questions, or suggestions please drop them in the comments. Also don’t forget if you like Return True & want to help out you can donate to the server fund or buy me a gift from my Amazon wish list using the links below.