dousi4472 2014-10-27 11:13
浏览 139
已采纳

VirtualDocumentRoot运行良好,但不对$ _SERVER [“DOCUMENT_ROOT”]进行任何更改

I use VirtualDocumentRoot directive for dynamice virtual hosts on apache, now my virtualhosts are working well but wen I check the value of $_SERVER["DOCUMENT_ROOT"] it still have the value of first DocumentRoot definition not VirtualDocumentRoot value !

the brief part of my "http.conf" file is:

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>

when I run this php code bellow

<?php
echo $_SERVER["DOCUMENT_ROOT"];
?>

I get /var/www/html as result. If I comment this line

#DocumentRoot "/var/www/html"

the PHP result would be

/etc/httpd/htdocs

But I want the value of $_SERVER["DOCUMENT_ROOT"] be like this:

/var/vhosts/exampleDomain.com

Is there any solution for this?

  • 写回答

1条回答 默认 最新

  • duanqiao9541 2014-10-27 11:25
    关注

    see 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄