Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualHost with wildcard VirtualDocumentRoot

I'm trying to create a fallback for my virtual hosts. My configuration looks like this:

# Fetch all pre-defined hosts

Include "conf/extra/vhosts/*.conf"

# Fallback

NameVirtualHost *:80

<Directory "C:/LocalServer/usr">
    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
<VirtualHost *:80>
    VirtualDocumentRoot "C:/LocalServer/usr/%-1/projects/%-2+/public/"
</VirtualHost>

The objective here is the following: If I try to access http://test.lab/, I want it to automatically pick up the following directory: C:/LocalServer/usr/lab/projects/test/public/.

Now, I have created the folders, and an empty index file (index.php). Nonetheless, Apache keeps showing me an empty Directory Index ("Index of").

No quite sure what to do now. Have tried a few things, none of which seem to work.

Any ideas?

Update - 1 June

I am now using this code, based on the first answer (well, the only one):

<VirtualHost *:80>
    UseCanonicalName Off
    ServerAlias *.lab
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
    <Directory "C:/LocalServer/%2/%1/public">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

I now get an Access forbidden error from Apache. One would surely, normally, receive this error when the directory does not exist? C:/LocalServer/lab/test/public does exist, and an empty index.php resides in the public directory.

The error in the general error log: [client 127.0.0.1:49342] AH01797: client denied by server configuration: C:/LocalServer/lab/test/public/

If I remove the <Directory/> group, nothing changes. I still get the error. (Can I even use %n in that group?)

Quick Note:

The reason it wasn't working before was due to the fact that I had other Virtual Hosts being imported, by means of the Include "conf/extra/vhosts/*.conf" instruction. Commenting it out (and thus making the Labs rule the only one) initiated the Access forbidden error.

Also note that I am no longer using the usr folder - each Lab is now in the lab folder, under LocalServer.

Update 2

It seems that the <Directory/> block does not allow for variables to be inserted, like VirtualDocumentRoot does.

Update 3 - Solution Found

It is now working - would not have been able to do it without the help. Here's the final code:

<VirtualHost lab:80>
    UseCanonicalName Off
    ServerAlias *.lab
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public"
    <Directory "C:/LocalServer/lab/*/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

Update 4 (April 2015)

New Directive, for those interested (using latest Apache 2.4):

<VirtualHost *:80>
    UseCanonicalName Off
    ServerAlias *.local
    VirtualDocumentRoot "D:/home/%-2+/public_html"
    <Directory "D:/home/*/public_html">
        Require all granted
        AllowOverride All
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

This, with the combination of Acrylic DNS Proxy, makes magic.

Update 5 (December 2016)

I'm now using a Macro approach.

# Directory Macro - Default Directory configuration on a per-vhost basis

<Macro Directory $dir>
    <Directory "z:/var/www/$dir/public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</Macro>

# LocalSub Macro - For specific *.*.local subs that require their own root

<Macro LocalSub $sub $domain>
    <VirtualHost 127.0.0.1>
        ServerName $sub.$domain.local
        DocumentRoot “z:/var/www/$domain/$sub/public_html”
        Use Directory $domain/$sub
    </VirtualHost>
</Macro>

Use LocalSub blog rockettpw

# Main virtual host

<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local *.*.local
    VirtualDocumentRoot “z:/var/www/%-2/public_html”
    Use Directory *
</VirtualHost>
like image 459
Mike Rockétt Avatar asked May 25 '13 06:05

Mike Rockétt


People also ask

What does VirtualHost *: 80 mean?

VirtualHost directive allows you to configure and use multiple sites located on the same IP address. In this case, with *:80 you are creating a virtual host for every request coming on the port 80. It becomes more interesting when you start specializing and start to insert something other than * in the virtual host.


1 Answers

I use them :) You forgot about switching off canonical names - unfortunately I don't know why there must be ServerAlias in my configuration - it just won't work without it - code below is tested and working

<Directory "C:/LocalServer/*/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require local
</Directory>

<VirtualHost *:80>
    # Apache will form URLs using the hostname supplied by the client
    UseCanonicalName Off

    # available aliases to use
    ServerAlias *.lab *.lab2

    # where to put them
    VirtualDocumentRoot "C:/LocalServer/%2/%1/public/"
</VirtualHost>
like image 188
Ochi Avatar answered Oct 28 '22 02:10

Ochi