dealing with VirtualHosts in XAMPP

If the URL schema of http://localhost/foldername isn’t working for you, or you just want to test the webpages behavior with the live address, you need to configure so called VirtualHosts in Apache. Usually that is done by editing httpd.conf but XAMPP has a different (and much easier) approach to this. Let’s take a look!

First you want to navigate to \apache\conf\extra inside your XAMPP’s install directory, and look for a file named httpd-vhosts.conf:

03-vhost-01

Open it in a text editor of your liking. Notice that everything is commented out, let’s just leave it as it is, and insert the following lines at the end of it:


    DocumentRoot "C:\xampp\htdocs\miniCRM"
    ServerName miniCRM.local
    ServerAlias www.miniCRM.local
    <Directory "C:\xampp\htdocs\miniCRM">
        Order allow,deny
        Allow from all

Notes:

  • I’m using miniCRM for this example and all folders are named as such. You need to change miniCRM in all the examples shown here to your-folder-name
  • I’m using .local as the top-level domain, but can you can use anything you want for example: .com, .org depending on your needs.

So the file should look something like this:

03-vhost-08

Now we need to restart Apache. From the XAMPP control panel Stop Apache:

03-vhost-03

And the Start it again:

03-vhost-04

It should start:

03-vhost-05

We need to edit one more file for the name resolution to work. If you have UAC enabled you want to start the text editor in elevated mode (Run as Administrator) as shown below:

03-vhost-06

Open the file named hostsĀ located underĀ \System32\drivers\etc of your Windows directory:

03-vhost-07

You can tweak name resolution from within this file. To do that, you can enter an IP Address (or range) DNS name pair separated by a space (or tab) per line. Here is an example:


127.0.0.1 miniCRM.local

This for example will bind the name miniCRM.local to 127.0.0.1 (which is the localhost’s IP Address). You can use any kind of domain name (even external ones) here, as this file will be parsed before the DNS servers. Here is how it should look like:

03-vhost-08

Save the file and you should be able to reach the address locally:

03-vhost-09

Leave a Reply

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