Apache – how to create an exception for a vhost’s VirtualDocumentRoot in httpd.conf

apache-2.2documentroothttpd.confvirtualhost

I'm new to Apache, and I'm wondering if there's a way to configure VirtualDocumentRoot to create an exception for just one of my vhosts on my local server. Currently all of my sites are stored in folders with the convention sitename.dev. In httpd.conf I'm dynamically setting VirtualDocumentRoot based on the server name:

# get the server name from the Host: header    
UseCanonicalName Off

LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /path/to/logs/access.log vcommon
ErrorLog /path/to/logs/errors.log

VirtualDocumentRoot /path/to/vhosts/%0

This works great for all of my sites with one exception… the document root for one of the sites should point to exception.dev/src/php instead of just exception.dev.

According to the answer I found here, it sounds as if VirtualDocumentRoot cannot be set based on a conditional. So I was hoping my problem could be resolved by setting an alias of some sort to point exception.dev to the right directory.

I've tried adding VirtualScriptAlias, but this prevents the Apache server from starting:

VirtualScriptAlias /path/to/vhosts/exception.dev /path/to/vhosts/exception.dev/src/php

I've also tried the same type of thing with ScriptAlias and just Alias, but still no luck.

Is there something I'm missing here? Or is what I'm trying to do impossible? Any help would be greatly appreciated!

Oh, and if it helps, here's my output if I run httpd -S:

httpd: Could not reliably determine the server's fully qualified domain name, using XXXXX.local for ServerName
VirtualHost configuration:
Syntax OK

UPDATE:
I got this to work by modifying Shane's answer below as follows:

NameVirtualHost *

<VirtualHost *:80>
    ServerName exception.dev
    DocumentRoot /path/to/vhosts/exception.dev/src/php
</VirtualHost>

<VirtualHost *:80>
    ServerName default
    ServerAlias *.dev
    VirtualDocumentRoot /path/to/vhosts/%0
</VirtualHost>

Best Answer

Have a virtual host with your "normal" config, the VirtualDocumentRoot, and another with the exception config.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName default
    ServerAlias *.dev
    VirtualDocumentRoot /path/to/vhosts/%0
</VirtualHost>

<VirtualHost *:80>
    ServerName exception.dev
    DocumentRoot /path/to/vhosts/exception.dev/src/php
</VirtualHost>
Related Topic