php - VirtualDocumentRoot 运行良好,但没有对 $_SERVER ["DOCUMENT_ROOT"进行任何更改]

标签 php apache config

我在 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/

相关文章:

PHP递归多维循环

php - 使用 php 变量值填充表单输入字段

php - 更改 Apache 配置目录?

java - 我想知道如何转换Spring mvc Java配置环境

php - 如何从具有特定条件的表中查找列名?

php - $_SERVER ['SERVER_ADDR' ] 总是设置吗?

php - 如何将 https 请求重定向到 https::www

java - org.apache.axiom.om 中的 "om"是什么意思?

go - 如何优雅地在 Golang 中制作配置文件?

javascript - Angular 中的提供者和实例有什么区别?