How to add a Virtual Host in XAMPP

When developing locally, I often like to separate my projects with a separate domain name like helgesverre.local and clientwebsite.local etc.

It’s fairly easy to get this working using XAMPP, however it might be daunting to try to research this on your own if you don’t know what you’re looking for, so I will teach you how to do it, ain’t I nice? :)

 

What you need to do is to open the following file in a text editor

C:\xampp\apache\conf\extra\httpd-vhost.conf

It will look something like this:

 

Delete everything in that file, and paste the following:

Don’t worry about the ErrorLog, CustomLog or ServerAdmin entry, they are not required for a Virtual Host to work.

 

The ServerName entry specified which domain to associate with the DocumentRoot, the DocumentRoot entry is the folder that will be loaded when visiting the domain specified by ServerName (in this example: my-project.local)

 

However we have not setup that domain to point to our local machine, if you try to visit my-project.local right now you will simply not get connected to anything.

 

So we have to open up our HOSTS file to add an entry that tells the computer to point that domain back to our own machine.

Open the following file in a text editor with Administrator rights, (right click -> Run as admin):

It will look something like this:

 

Simply add the following line at the very bottom of the file.

This will route all requests to my-project.local to the IP Address 127.0.0.1 which is the loopback address of the machine, which just points to itself.

You are of course free to change my-project.local to whatever you want, but it needs to be the same in both the hosts file and the virtual host configuration file (httpd-vhost.conf).

 

If you have Apache running, you need to restart it so it can load the new configuration.

When navigating to my-project.local you should now be routed to your local web server and see the contents of the folder you specified in the VirtualHost entry.

Hint: the directory must exist or else you will get an error.

 

If you have any questions, feel free to leave a comment and I will get back to you.

1 comment

Leave a Reply

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