Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm not sure if that question was properly worded.

Basically, I have it set up via http://httpd.apache.org/docs/2.2/vhosts/mass.html so that I only have to create a folder (such as example.test) in my ~/Sites folder and I can go to http://example.test (granted my hosts file matches).

I am working on a couple projects that have a framework installed at one level above the public directory, so I was wondering if I could override the mass vhost for a single vhost.

I tried the following:

# I want this...
NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /Users/cameron/Sites/laravel.test/public
    ServerName laravel.test
</VirtualHost>

# ...to override this only for laravel.test
UseCanonicalName Off
VirtualDocumentRoot /Users/cameron/Sites/%0

The Laravel PHP framework installs the framework in level above the public like I was talking about, so when I go to http://laravel.test, I want it to go to /Users/cameron/Sites/laravel.test/public instead of /Users/cameron/Sites/laravel.test/ if that makes sense...

But the above code doesn't work. It basically ignores anything before the last two lines.

So my question is... is there a way to achieve what I want? Not necessarily the approach I'm trying, but I have a ton of local sites and 99% of them work great under the mass virtual host approach, I just occasionally want to override that behavior.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The documentation for VirtualDocumentRoot says to use none to turn it off, so I assume what you need to do is add that to your <VirtualHost> section:

<VirtualHost *:80>
    DocumentRoot /Users/cameron/Sites/laravel.test/public
    VirtualDocumentRoot none
    ServerName laravel.test
</VirtualHost>

This isn't enough, though: when using NameVirtualHost, if an incoming request does not match the ServerName/ServerAlias of any <VirtualHost> block, then the first <VirtualHost> block is used, no matter what name it had. In this case, it causes all of the other hostnames to this VirtualHost directive too.

A possible way to fix this would be to move the VirtualDocumentRoot directive into a <VirtualHost> block placed in the configuration before any other sites. The ServerName of this block shouldn't matter, something like:

<VirtualHost *:80>
     ServerName bogusname
     UseCanonicalName Off
     VirtualDocumentRoot /Users/cameron/Sites/%0
</VirtualHost>

The bogus name should ensure that this VirtualHost will only be used in the "default" case of no other VirtualHosts matching the request.

share|improve this answer
    
This is making every site I have go to that DocumentRoot though, and not just laravel.test. Why is that? Shouldn't it only do the other stuff (i.e. turn off VirtualDocumentRoot) if ServerName is laravel.test? I am obviously missing something. –  CWSpear Aug 15 '12 at 0:46
    
@CWSpear yeah, my answer is wrong. I'll update it to explain why –  DerfK Aug 15 '12 at 1:08
    
This worked exactly like I wanted it to. Thank you very much! –  CWSpear Aug 15 '12 at 4:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.