【问题标题】:$_SERVER['DOCUMENT_ROOT'] is returning a different value after a rewrite rule$_SERVER['DOCUMENT_ROOT'] 在重写规则后返回不同的值
【发布时间】:2019-10-02 19:50:40
【问题描述】:

我已经做了很多谷歌搜索,尝试了不同的东西,但我仍然没有弄清楚为什么我的 $_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

【问题讨论】:

  • 输入是指输出,不是吗?
  • 糟糕!我的错,我要编辑我的问题。你是对的,我的意思是输出不是输入。

标签: php apache mod-rewrite


【解决方案1】:

试试这个:

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

在您的“.htaccess”文件中。

【讨论】:

  • 感谢您试一试。我在d:\data\v5.3\test\code 中创建了一个新的 .htaccess,其中包含上面的代码,但没有运气。我仍然得到相同的结果
猜你喜欢
  • 2012-09-28
  • 1970-01-01
  • 2013-08-12
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多