How to configure Virtual Host in Xampp – royalcode.eu

How to configure Virtual Host in Xampp

Some time ago I installed Windows 10 next to my beloved Open Suse. Sensing the opening knives in the pockets of the Linux community I will defend myself that I need it for C# and .NET exercises. Sometimes, however, it makes no sense to switch between systems, so to quickly put something locally, you could use a virtual host on Xampp.

What is Virtual Host

By Virtual Host we mean hosting multiple domains on one server. So you need a virtual address that will behave exactly like on the Internet. Thanks to this you will be able to use absolute paths. Typed “/” will refer to root, which is the directory where the domain is parked.

Setting up and configuring Virtual Host

To start with, if you have already tried to change something in Xampp files, delete it, remove any leftovers and reinstall the latest version. In the meantime, create a folder for virtual hosts (pages that you will have in parallel). In this case, I did it directly on disk C, under the name “vhost”.

Make sure the new Xampp is turned off. For convenience, I create shortcuts on the desktop for files and folders, which I will sometimes edit:

  • hosts (C:\Windows\System32\drivers\etc)
  • Directory vhost (C:\vhost)
  • Directory htdocs (C:\xampp\htdocs)
  • httpd-vhosts.conf (C:\xampp\apache\conf\extra\httpd-vhosts.conf)

At first open the file httpd.conf (C:\xampp\apache\conf\httpd.conf) and search for the code below:

<Directory />
    AllowOverride none
    Require all denied
</Directory>

Change “none” to “all”.

<Directory />
    AllowOverride all
    Require all denied
</Directory>

Save file. Open the “vhost” directory you created earlier and add subdirectories for the files of the new sites. Let’s assume that we want to add two new websites: strona1.pl and strona2.pl.

Vitual Host how to bet Virtual Host

Now open file httpd-vhosts.conf (C:\xampp\apache\conf\extra\httpd-vhosts.conf) and add virtual hosts with the following configuration scheme:

<VirtualHost *>
DocumentRoot "c:/vhost/strony1"
ServerName strony1.pl
ServerAlias www.strony1.pl
<Directory "c:/vhost/strony1">
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

<VirtualHost *>
DocumentRoot "c:/vhost/strony2"
ServerName strony2.pl
ServerAlias www.strony2.pl
<Directory "c:/vhost/strony2">
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

If you have the commented code above, make sure the line listed is not commented out and you removed the port:

NameVirtualHost *

Finally, assign the IP to the domain. Open the hosts file (C:\Windows\System32\drivers\etc) and add two lines at the end:

127.0.0.1 strony1.pl
127.0.0.1 strony2.pl

Ready. 🙂 Restart the browser. After placing the files in the appropriate directories and enabling Xampp, you will be able to conveniently operate on several locally placed pages simultaneously.