How can I update my php DOCUMENT_ROOT variable from VirtualDocumentRoot

I am using VirtualDocumentRoot in my Virtual host configuration.

My VirtualDocumentRoot is set to /var/www/%1/ and if I create a folder say test, it is working as expected. The problem is, when I am using $_SERVER["DOCUMENT_ROOT"] in my php files it is retuning /var/www. Is it possible to have php return the same path for $_SERVER["DOCUMENT_ROOT"] as configured in VirtualDocumentRoot.


If you can edit your PHP, you could calculate the correct DOCUMENT_ROOT and update the variable on the bootstrap. Here is an example: http://blog.suffolk-web-design.co.uk/2008/09/apache-automatic-sites-using.html


Following the advice in the link in @Thomas' answer, this is how I dealt with this;

In my dynamic virtual host directive in httpd.conf, I added this php prepend;

php_admin_value auto_prepend_file /var/www/html/uat.mydomain.co.uk/update_doc_root.php

Then in the root of my uat directory, I saved a little file that is prepended to all requests met by the dynamic virtual host entry (not all hosts on this server are dynamic, and I don't want to mess with the other ones). In it, I put this;

<?php
$path = explode("/",str_replace($_SERVER['DOCUMENT_ROOT'],"",$_SERVER['SCRIPT_FILENAME']));
$_SERVER['DOCUMENT_ROOT'] .= "/" . $path[1] . "/" . $path[2];

This works for me because my VirtualDocumentRoot is two levels beneath my httpd document root. If yours is any deeper, you will need to adjust the indices of the $path array.

Hope this helps others with the same issue...