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 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)