Question

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>
Was it helpful?

Solution

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>

OTHER TIPS

Given that you are obviously using windows for development, but (presumably) deploying to linux for production, have you thought about using a virtual machine for development?

I've written a guide for setting up here: http://otaqui.com/blog/1652/setting-up-a-virtualbox-virtual-machine-for-web-development-with-multiple-sites-using-mod_vhost_alias-and-virtualdocumentroot/ but in essence:

  • Share a directory (e.g. C:\VirtualWWW) from the HOST to the GUEST
  • Mount that share as /var/www in the GUEST, with www-data as the owner
  • Setup vhost_alias and VirtualDocumentRoot to map subdirectories in C:\VirtualWWW to virtual host subdomains, i.e. C:\VirtualWWW\project1 is mapped to http://project1.vhost/

Setting up new projects is then as simple as creating a new directory on your host, and the virtual machine guest uses that. If you are deploying to linux, you might save yourself all sorts of headaches (filename case-sensitivity being only one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top