Windows XAMPP Vhost And Custom Domain Setup

Hristiyan Dodov
Jun 26 · 2 min read

I’m going to show how I set up XAMPP so that I can use http://myproject.test as the domain for locally developing a PHP project that is also outside the htdocs folder.

Edit The Hosts File

First of all, we need to map the myproject.test domain to XAMPP. Right now, if you attempt to open http://myproject.test in the browser, it’ll attempt to resolve the domain name to an IP address via DNS and fail.

Luckily, there’s a HOSTS file that can contain pre-mapped IP-domain pairs. This means we can explicitly set that a certain domain resolves to a certain IP address. In this case, we want myproject.test to be mapped to 127.0.0.1 , which is localhost . From there, Apache would do the rest.

To do that, open C:\Windows\System32\drivers\etc\hosts and make sure you open it as Administrator because it’s a system file and has higher security.

At the bottom of the file, add:

127.0.0.1 myproject.test

Then, save the hosts file. The changes should take effect immediately. If they don’t, try restarting your browser.

Now, if you open http://myproject.test in your browser, you should see the XAMPP dashboard (make sure Apache is running, though).

Verify That The Vhosts File Is Loaded

Open Apache’s main configuration file, httpd.conf . In my case, it was located at C:/xampp/apache/conf/httpd.conf. Then, search for this:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Make sure this httpd-vhosts.conf file is included. We will put our virtual host configurations there.

Set Up a Virtual Host

After you’ve verified that httpd-vhosts.conf is included, open it. Read the comments inside for some useful information and add the following block:

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

This is just a VirtualHost directive for the default htdocs host behavior. We need this because as the Apache docs state:

If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host.

Since we are going to add virtual hosts, our default host would go away. If you don’t need it, you also don’t need the VirtualHost directive above.

We need to add the VirtualHost directive for our project now. For this example, I’ll put my project on my Desktop at C:/Users/USER/Desktop/test . Then, add the following to httpd-vhosts.conf :

<VirtualHost *:80>
ServerName myproject.test
DocumentRoot "C:/Users/USER/Desktop/test"
</VirtualHost>
<Directory "C:/Users/USER/Desktop/test">
Require all granted
</Directory>

The VirtualHost directive specifies the name that the server uses to identify itself and its document root. The Directory directive gives access to the root folder. Otherwise, we would receive 403 errors.

Testing

After everything is set up, add an index.php file in your project folder and restart Apache so the virtual host configurations can be applied. Then, open http://myproject.test and you should see your project. At the same time, http://localhost should open the XAMPP dashboard located in htdocs .

Conclusion

You can easily set up a nice workflow for you PHP projects by adding a custom domain to Windows’ HOSTS file and adding small configurations for Apache.

To add more projects in the same manner, repeat the same steps. Or, you can add your projects in htdocs as always.

Oblik Studio

a cross-disciplinary design studio and the people behind it

Hristiyan Dodov

Written by

Full-Stack Developer at Oblik Studio

Oblik Studio

a cross-disciplinary design studio and the people behind it

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade