首页 > 解决方案 > $_SERVER['DOCUMENT_ROOT'] 在重写规则后返回不同的值

问题描述

我做了很多谷歌搜索,尝试了不同的东西,但我仍然没有弄清楚为什么我的 $_SERVER['DOCUMENT_ROOT'] 会改变。我认为这与这条线有关

RewriteRule ^(.*)$ D:/data/v5.3/%1/%2/$1 [L]

访问http://test.example.com时,输出

<?php echo $_SERVER['DOCUMENT_ROOT']; ?> is "D:\data\v5.3\test\www"

当访问http://test.redesign.example.comhttp://test.code.example.com的输出

<?php echo $_SERVER['DOCUMENT_ROOT']; ?> is "D:/data"

小路:

d:\data\v5.3\test\

文件夹结构:

- code
- redesign
- www

这就是我在 httpd-vhosts.conf 中的内容:

<IfModule mod_vhost_alias.c>
  <VirtualHost *:80>
    ServerAlias *
    UseCanonicalName Off
    VirtualDocumentRoot "D:/data/v5.3/%1/www"

        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^(.*?)\.(code|redesign|new|old)\.(.*)$ [NC]
        RewriteRule ^(.*)$ D:/data/v5.3/%1/%2/$1 [L]

    <Directory "D:/data/v5.3">
      AllowOverride All
    </Directory>

  </VirtualHost>
</IfModule>

访问http://test.example.com时,输出

<?php echo $_SERVER['DOCUMENT_ROOT']; ?> should be "D:\data\v5.3\test\www" (this is fine)

当访问http://test.redesign.example.comhttp://test.code.example.com的输出

<?php echo $_SERVER['DOCUMENT_ROOT']; ?> should be "D:\data\v5.3\test\redesign" or 
"D:\data\v5.3\test\code" (depending on what's in URL after test.)

注意

dirname(__FILE__) is working just fine, so it's only an issue 
with $_SERVER['DOCUMENT_ROOT'] which some of my websites might rely on

标签: phpapachemod-rewrite

解决方案


尝试这个 :

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

在您的“.htaccess”文件中。


推荐阅读