Create dynamic virtual hosts on XAMPP with Windows

Posted on

I just tested it and it still works in 2020 with the newest XAMPP Version.

This is a short tutorial how the get up and running with dynamic virtual hosts with XAMPP on Windows. The goal of this tutorial is to be able to create a directory like “local.mywebsite.com” in your C:\xampp\htdocs\ to add a virtual host dynamically.

First of we have to install XAMPP if you haven’t already. Please install it into the default folder C:\xampp because this will save us some typing later on.

After the XAMPP installation is finished we have to activate the vhost module in Apache. To do this you have to open the file C:\xampp\apache\conf\httpd.conf and remove the # at the beginning of this line.

LoadModule vhost_alias_module modules/mod_vhost_alias.so

Next paste the following snippet into your C:\xampp\apache\conf\extra\httpd-vhosts.conf file.

<VirtualHost *:80>
    UseCanonicalName Off
    ServerAlias *
    VirtualDocumentRoot "C:/xampp/htdocs/%0"
    VirtualScriptAlias "C:/xampp/htdocs/%0"
    LogLevel debug
    <Directory "C:/xampp/htdocs/">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require local
    </Directory>
</VirtualHost>

And that’s it. Now we just have to test it out. To see error when starting apache it’s best not to start it with the XAMPP panel but to start it by executing the  C:\xampp\apache_start.bat script. The script will show you if there are any problems with your configuration when starting apache.

If you run into problems at this point leave a comment with your error message and I’ll try to help you. (Except if you’ve installed your XAMPP into the programs folder, in this case I can’t help you any longer.)

When there aren’t any problems you can start Apache from now on with the XAMPP panel. If you now want to add a host you can just create the folder in your C:\xampp\htdocs\ folder and add a entry to your C:\Windows\system32\drivers\etc\hosts file. (To edit this file open your text editor first with admin rights.)

127.0.0.1 beta.eggworks.ch

Now you’re all setup. If you have any comments, proposals or ideas how to improve this article feel free to comment below.

7 thoughts on “Create dynamic virtual hosts on XAMPP with Windows

  1. Hi this, works great.
    But what about running it with subdomains?
    Like having a directory called “example.com” and being able to run username.example.com and it still points to the “example.com” directory.

    1. You could do that by customizing the VirtualDocumentRoot and VirtualScriptAlias. Using %0 means the directory name is the full domain name including subdomain. If you want to ignore subdomains you could change this parameter to only include the domain.

      The example in the docs is

      VirtualDocumentRoot “/usr/local/apache/vhosts/%3+/%2.1/%2.2/%2.3/%2”

      So a request for http://www.domain.example.com/directory/file.html will be satisfied by the file /usr/local/apache/vhosts/example.com/d/o/m/domain/directory/file.html.

      By customizing the parameters you could make it ignore the subdomain.

      https://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html#page-header

      1. The example in the docs might fit your usecase:

        “A very common request by users is the ability to point multiple domains to multiple document roots without having to worry about the length or number of parts of the hostname being requested. If the requested hostname is sub.www.domain.example.com instead of simply http://www.domain.example.com, then using %3+ will result in the document root being /usr/local/apache/vhosts/domain.example.com/… instead of the intended example.com directory. In such cases, it can be beneficial to use the combination %-2.0.%-1.0, which will always yield the domain name and the tld, for example example.com regardless of the number of subdomains appended to the hostname. As such, one can make a configuration that will direct all first, second or third level subdomains to the same directory:

        VirtualDocumentRoot “/usr/local/apache/vhosts/%-2.0.%-1.0”

        In the example above, both http://www.example.com as well as http://www.sub.example.com or example.com will all point to /usr/local/apache/vhosts/example.com.”

    1. This depends on your other vhost setup. It is possible to configure this setup to only map certain tld’s into the directory.

      You’d have to try it out yourself and then tell me if there is any error.

Leave a Reply