php - $_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.com和http://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
解决方案
尝试这个 :
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
在您的“.htaccess”文件中。
推荐阅读
- javascript - 如何在 JSON 响应中存储多对多 Django 字段的值?
- javascript - 当应用程序进入后台 WKWebView 时如何评估 JavaScript?
- python - Python3:安装 google colab 时出错
- python - 常规和命名空间包的 sys.path 的导入顺序
- python - 我得到了 python BeautifulSoup 的结果集,但我不知道如何获取其中的 NavigableString
- python - Tkinter,如何使散点图弹出到新窗口
- gatsby - 如何在 gatsby 网站上下载外部文件?
- html - 从容器中分离滚动
- python - 如何使用 model.py 或任何替代选项将打印数据(即终端的 odr_id)添加到数据库?
- sql - 我如何选择每对不同的两列的第一条记录