Remap request to different local path
<p>I'd like to serve multiple subdomains with my webserver:</p>

<p>example.com<br>
projects.example.com<br>
blog.example.com<br>
wiki.example.com<br>
...</p>

<p>The document root is being set as follows:</p>

<pre><code>ServerName   example.com
ServerAlias   *.example.com
VirtualDocumentRoot "/var/www/%0/public"
</code></pre>

<p>This works as it automatically sets the document root to the corresponding subdomain. My next requirement however, is that it should detect if the <code>public</code> folder in the document root even exists, and if not, serve the file from one directory earlier.</p>

<p>Most of my applications are written with Laravel, so they come with a <code>public</code> folder, however sometimes I just want to quickly create a new script that doesn't have <code>public</code> or deploy external software that doesn't.</p>

<p>Therefore the document root for <code>example.com</code> (which just serves a static <code>index.html</code> and <code>style.css</code>) should be <code>/var/www/example.com/</code> rather than <code>/var/www/example.com/public</code>. I tried this via rewrite rules:</p>

<pre><code>&lt;If "!-d '/var/www/' . %{HTTP_HOST} . '/public'"&gt;
    RewriteEngine on
    RewriteRule ^ "/var/www/%{HTTP_HOST}" [L,QSA]
&lt;/If&gt;
</code></pre>

<p>This is the result of the rewrite error log:</p>

<pre><code>[rewrite:trace2] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] init rewrite engine with requested uri /
[rewrite:trace1] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] pass through /
[rewrite:trace3] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] [perdir *If/] add path info postfix: /var/www/example.com -&gt; /var/www/example.com/public/
[rewrite:trace3] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] [perdir *If/] applying pattern '^' to uri '/var/www/example.com/public/'
[rewrite:trace2] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] [perdir *If/] rewrite '/var/www/example.com/public/' -&gt; '/var/www/example.com'
[rewrite:trace1] [pid 32153] mod_rewrite.c(475): [client 62.159.226.12:27139] 62.159.226.12 - - [example.com/sid#7f6588016340][rid#7f6587f810a0/initial] [perdir *If/] initial URL equal rewritten URL: /var/www/example.com [IGNORING REWRITE]
</code></pre>

<p>As you can see, it maps it correctly, but for some reason doesn't serve the file from there (I assume it's because it doesn't append <code>index.html</code> or anything the like).</p>

<p>How do I get around this?</p>