VirtualDocumentRoot FTW!

One thing I got to hear about at DrupalCon during the Q&A at Sacha Chua's Totally Rocking Your Development Environment was someone who popped up quick and mentioned the utility of VirtualDocumentRoot, a directive for Apache which lets you automatically go find the folder to server the site out of based on the incoming host header.

I created one VirtualHost called all.conf. The virtual host has a ServerAlias * (I'm sure there's a cleaner way to do this, like just in the default config, but I already had this pretty close, so this was a bit of magic). My all.conf looks like this, though you probably don't need the <Directory> portion.

&lt;VirtualHost *&gt;
  UseCanonicalName Off
  VirtualDocumentRoot /var/www/%0
  ServerAlias *
  LogLevel Debug
  &lt;Directory "/var/www/%0"&gt;
    AllowOverride all
    allow from all
    Options +Indexes
  &lt;/Directory&gt;
&lt;/VirtualHost&gt;

The %0 says "look for a folder with exactly what's typed in as the host header" (so not good if you want www.domain.com and domain.com served from the same, for that we used VirtualDocumentRoot "/var/www/%-2.0.%-1/").

So now, I just create a new folder, i.e. 'redfinsolutions' inside /var/www, then I go over to /etc/hosts (or c:\windows\system32\drivers\etc\hosts) and add "127.0.0.1 redfinsolutions" and voila!

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4> <h5> <h6> <img>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options