Skip to content

Instantly share code, notes, and snippets.

@tjboudreaux
Created August 19, 2011 16:35
Show Gist options
  • Save tjboudreaux/1157278 to your computer and use it in GitHub Desktop.
Save tjboudreaux/1157278 to your computer and use it in GitHub Desktop.
Fixes broken $_SERVER['DOCUMENT_ROOT'] with Apache VirtualDocumentRoot
/**
* URL Patterns to look for
* This function is used to fix the document root for sites that are using
* one of my VirtualDocumentRoot site entries
* This is to fix a bug in Apache's mod_vhost_alias not setting up the correct
* $_SERVER['DOCUMENT_ROOT'] env variable.
*
* trunk.{directory}.local - maps to /Users/tjboudreaux/Sites/{directory}/trunk
* branch.{branch-name}.{directory}.local - maps to /Users/tjboudreaux/Sites/{directory}/branches/{branch-name}
*
* @author Travis Boudreaux
* @link http://www.devcha.com/2010/02/solved-incorrect-value-for.html
*/
$host = explode(".",$_SERVER['HTTP_HOST']);
if ($host[sizeof($host)-1] == "local")
unset($host[sizeof($host)-1]);
$suffix = "";
if ($host[0] == "trunk") {
unset($host[0]);
$suffix ="/trunk";
} elseif ($host[0] == "branch") {
$suffix = "/branches/" . $host[1];
unset($host[0]);
unset($host[1]);
}
$fixed_vhost_dir = "/User/tjboudreaux/Sites/" . implode(".",$host) . $suffix;
if (is_dir($fixed_vhost_dir) && $_SERVER['DOCUMENT_ROOT'] == '/Library/WebServer/Documents') {
$_SERVER['__MOD_VHOST_FIX_OLD_DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'];
$_SERVER['DOCUMENT_ROOT'] = $fixed_vhost_dir;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment