我在 apache 上使用 VirtualDocumentRoot 指令来动态虚拟主机, 现在我的虚拟主机运行良好 但当我检查 $_SERVER["DOCUMENT_ROOT"] 的值 它仍然具有第一个 DocumentRoot 定义的值 不是 VirtualDocumentRoot 值!
我的“http.conf”文件的简短部分是:
UseCanonicalName Off
DocumentRoot "/var/www/html"
<VirtualHost *:80>
ServerAdmin admin@localhost
VirtualDocumentRoot "/var/vhosts/%0"
ErrorLog "logs/dynamic-vhosts-error.log"
CustomLog "logs/dynamic-vhosts-access.log" combined
</VirtualHost>
当我运行下面的 php 代码时
<?php
echo $_SERVER["DOCUMENT_ROOT"];
?>
我得到/var/www/html 结果。 如果我评论这一行
#DocumentRoot "/var/www/html"
PHP 结果将是
/etc/httpd/htdocs
但我希望 $_SERVER["DOCUMENT_ROOT"] 的值是这样的:
/var/vhosts/exampleDomain.com
有什么解决办法吗?
最佳答案
参见http://joshbenner.me/blog/quick-tip-get-proper-document-root-when-using-mod-vhost-alias/
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.
关于php - VirtualDocumentRoot 运行良好,但没有对 $_SERVER ["DOCUMENT_ROOT"进行任何更改],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26586317/