VirtualHost for Local Domains
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.
- Open up notepad as administrator, by finding Notepad under the Start Menu, right clicking and “Run as administrator”.
- Go to Open, and find the file
C:\Windows\System32\drivers\etc\hosts
- Make sure “All Files” in the open dialog is selected, and not just “*.txt”.
- 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.