$ _SERVER ['DOCUMENT_ROOT']在重写规则后返回了另一个值

时间:2019-10-02 19:50:40

标签: php apache mod-rewrite

我做了很多谷歌搜索,尝试了不同的东西,但是我仍然没有弄清楚为什么我的$ _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.com时,或 http://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

1 个答案:

答案 0 :(得分:0)

尝试:

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

在您的“ .htaccess”文件中。