Apache 2.2 – How to Set VirtualDocumentRoot Based on Files

apache-2.2mod-rewrite

I'm trying to set up Apache to use the VirtualDocumentRoot directive but my sites aren't all exactly the same. Most of the sites have a drupal folder which should be the root but there are a few really old drupal sites, a few rails sites, some django sites, etc. that want the Document root to be / or some other folder.

Is there a way to set up VirtualDocumentRoot based on a conditional or is there a way to use RewriteRule/Cond to detect that / is the incorrect folder if there is a drupal folder or a public folder?

Here's what I have so far:

<VirtualHost *:80>
  # Wildcard ServerAlias, this is the default vhost if no specific vhost matches first.
  ServerAlias *.unicorn.devserver.com

  # Automatic ServerName, based on the HTTP_HOST header.
  UseCanonicalName Off

  # Automatic DocumentRoot.  This uses the 4th level domain name as the document root,
  # for example http://bar.foo.baz.com/ would respond with /Users/vosechu/Sites/bar/drupal.
  VirtualDocumentRoot /Users/vosechu/Sites/%-4/drupal
</VirtualHost>

Thanks in advance!

-Chuck

Best Answer

Is there a way to set up VirtualDocumentRoot based on a conditional?

No -- That's simply not how VirtualDocumentRoot works -- It picks apart a portion of the hostname and sticks it into a path.


or is there a way to use RewriteRule/Cond to detect that '/' is the incorrect folder if there is a drupal folder or a public folder?

I'm not 100% sure what you're asking here, but it sounds like "Is there a way to make Apache behave differently based on the contents of a directory?" - If so the answer is pretty much "No":
Apache just serves what it's given. It has know way of "knowing" if a directory is a Drupal site (It just sees a PHP file, and fires up a PHP interpreter because that's what it's been told to do with PHP files, and spits out the results the interpreter gives it back).


You seem to be trying to shove the square peg of your radically different sites into the round hole of VirtualDocumentRoot (which is really best suited to dead simple sites) -- In your case I think you really need to create separate virtual hosts for each site and manage their configurations separately.