Oct
25
Quick Tip: Get proper DOCUMENT_ROOT When Using mod_vhost_alias
October 25, 2009 - 4:47pm
The Apache module mod_vhost_alias and its VirtualDocumentRoot directive can really be a great time saver for local development (some googling will explain why in more deapth). Basically, my local dev is set up so that I just have to create a directory in my aliases directory, and I just then navigate my browser to a URL matching the name of that new directory, and apache knows exactly what to serve automagically.
However, there are a few evil gotchas when using mod_vhost_alias, one of which is that the PHP global $_SERVER['DOCUMENT_ROOT'] remains set to the apache default DOCUMENT_ROOT environment variable rather than being re-assigned to the document root activated by the VirtualDocumentRoot directive for the current URL. This can cause some PHP applications (that are too trusting) to die for one reason or another.
I found a great solution to this in the related apache bug report: Simply add the following line to your apache configuration inside the VirtualDocumentRoot vhost definition:
php_admin_value auto_prepend_file /path/setdocroot.php
Then, create the referenced PHP file, and put set this as its contents:
<?php $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
Now, every page load has this file executed, which properly sets DOCUMENT_ROOT.
Great solution. Thank you for the tip!
This can be improved to allow getenv() to work correctly too. If your document root isn't being set correctly even with the solution above, try putting this in instead:
Thanks very much for this fix to a very annoying problem. The solution from Rob did the trick for me.
Dave
Hmm, good idea! But alas, after trying this I discovered it will not work for me.
I'm having the same problem, but I think my setup is more complicated. Not only do I reference $_SERVER['DOCUMENT_ROOT'] from PHP, but my .htaccess file uses DOCUMENT_ROOT when doing fancy redirects. I need a way to set this variable from apache before it reaches my .htaccess file and before it reaches PHP.
Any ideas?
Andrew, there is a way if you are prepared for an additional step.
For each virtual host that you need the DOCUMENT_ROOT to be properly set by apache, you can add the following above your 'catchall' virtual host definition.
<VirtualHost *:80>ServerName test.local
ServerAlias *.test.local
DocumentRoot /path/to/the/site/root
</VirtualHost>
This way the DOCUMENT_ROOT variable for the specified ServerName will be manually set and will override the server's default DOCUMENT_ROOT.
Note: You will also need to restart Apache after adding a new definition.
@george kom -- may not work too well if you are using mod_vhost_alias.
Your script didn't work for me if I was using an alias for my directory as the alias path did not exist. I made this one up instead which works for me:
Thanks Josh!
Thx man! Saved me a lot of time!
This does not work on some common setups like HostGator, for which the document root will be something like /usr/local/apache/htdocs, the script filename will be /home/user/public_html/... and the script name may be something like /~user/...
Post new comment