VirtualHost for Local Domains

Our site remains ad-free thanks to our affiliates. This post's featured affiliate is Fiverr. Fiverr connects businesses with freelancers offering digital services in 300+ categories.

The post is not sponsored by the above affiliate and the content here is not representative of their company.

If you are working on many sites on a localhost, you might want to assign “Fake Domains” to each of those sites. Or if you need to point to a site instead of the file path, this tutorial will help you.

First, you need to have Apache installed, this can be installed using software packages such as XAMPP.

Go to your Apache Configuration File, which is located under C:\xampp\apache\conf\httpd.conf

This location will vary on where you installed XAMPP.

Now, in this file you need to create a VirtualHost for each “domain” you want, and create one for localhost.

NameVirtualHost *:80
<VirtualHost *:80>
	DocumentRoot "C:/xampp/www"
	ServerName localhost
</VirtualHost>

Since we have localhost setup, we can add all the extra domains we want, for example “domain.tld”.

<VirtualHost *:80>
	DocumentRoot "C:/xampp/www/domain.tld"
	ServerName domain.tld
</VirtualHost>

You can repeat the code above with as many domains as you want, and the DocumentRoot can be any location you want, that you have permissions for.

There is one last step, which is to edit the HOSTS file in Windows to allow the “domain.tld” to point to localhost.

  1. Open up notepad as administrator, by finding Notepad under the Start Menu, right clicking and “Run as administrator”.
  2. Go to Open, and find the file C:\Windows\System32\drivers\etc\hosts
  3. Make sure “All Files” in the open dialog is selected, and not just “*.txt”.
  4. On the bottom of the HOSTS file, you want to add this line for each domain you want pointed to your server. If you are using real domain names, access to those domains will no longer be allowed, since it will now point to your local server.
127.0.0.1 domain.tld

We map domain.tld to 127.0.0.1 (which is localhost).

There you have it, now you can access your site by going to domain.tld instead of localhost/domain.tld, and you can do this for any domain you want.

Related Posts

How to Train SpamAssassin

Learn about the different methods used to train SpamAssassin, along with initial spam data sources to use with SpamAssassin. Update your bayes database easily with existing data.

SpamAssassin SA-Update Tool

Learn what SpamAssassin's sa-update tool does, how it works, and if you should keep it running and modifying the configuration files on your server.

Incremental MySQL Backup with Binary Log

Learn how to properly perform an incremental MySQL backup using binary logs without having a gap between backups and overall improve the speed of database backups.

Website Admin Panel on Private Network

Split your website into two parts, administrative and public access. This post talks a little about having your website's control panel on your private network as apposed to publicly available.