How to have dynamic virtual hosts for all domains except localhost
<p>A bit of background: I've been wanting to set up dynamic virtual hosts for a while now. Basically I just wanted to be able to plop a folder into my virtual hosts folder and just have it work without any other configuration. I found out that in chrome any subdomain of <code>.localhost</code> will behave the same as <code>localhost</code>. This means that I can use <code>.localhost</code> as a TLD for all my projects and I don't have to edit my <code>HOSTS</code> file for every new virtual host I want to add.</p>
<p>I read the documentation on <a href="https://httpd.apache.org/docs/current/vhosts/mass.html" rel="nofollow noreferrer">https://httpd.apache.org/docs/current/vhosts/mass.html</a> and figured out how to have dynamic virtual hosts based on the host header.</p>
<p>After reading through that page and other resources on the internet, I came up with the following configuration in my <code>httpd-vhosts.conf</code> file. This uses the part before the <code>.localhost</code> to determine the folder name.</p>
<pre><code>&lt;VirtualHost *:80&gt;
    ServerAdmin admin@localhost
    
    # Get the server name from the Host header
    UseCanonicalName Off
    
    # Log
    LogFormat &quot;%V %h %l %u %t \&quot;%r\&quot; %s %b&quot; vcommon
    CustomLog logs/vhost_access.log vcommon
    ErrorLog logs/vhost_error.log
    
    # Match domain name against a folder
    VirtualDocumentRoot &quot;C:/vhosts/%-2+&quot;
    
    &lt;Directory &quot;C:/vhosts/*&quot;&gt;
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</code></pre>
<p>So with this set-up, I can create a new folder in the <code>vhosts</code> folder that contains all my html documents. So, for instance, if I create a folder named <code>project1</code> I can access that folder by going to <code>http://project1.localhost</code>.</p>
<p>Now my main problem is that I don't always need to create a new virtual host. I just want to create a random php file and access it by going to <code>http://localhost/index.php</code>. However, with the above configuration, just using <code>http://localhost</code> caused an error. Probably because the pattern in the <code>VirtualDocumentRoot</code> directive is using the subdomain of <code>localhost</code> and there is no subdomain when I just use <code>http://localhost</code>.</p>
<h2>tl;dr:</h2>
<p><strong>Given the configuration above, is there any way to have a hard-coded virtual host for <code>localhost</code> and have dynamic virtual hosts for subdomains of <code>localhost</code>?</strong></p>
<p><strong>Or, how can I create a configuration that would allow me to have this:</strong></p>
<pre><code>http://localhost -----------------&gt; C:/vhosts/
http://project1.localhost --------&gt; C:/vhosts/project1
http://project2.localhost --------&gt; C:/vhosts/project2
http://blog.project2.localhost ---&gt; C:/vhosts/project2/blog
</code></pre>