由于某些原因,我的.htaccess
正在为_url
密钥添加默认值。
我的.htaccess
AddDefaultCharset UTF-8
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
当我在http://localhost/my-project
上访问我的项目并转储查询字符串var_dump($_GET);
时,我得到以下结果;
array(1) {
["_url"]=>
string(11) "/index.html"
}
当我在http://localhost/my-project/test
上访问我的项目并转储查询字符串时,我得到以下结果;
array(1) {
["_url"]=>
string(11) "/test"
}
我猜测它与我的.htaccess和/或服务器(apache)配置有关。
这个问题有解决方法吗?为什么我没有预期的结果;
array(1) {
["_url"]=>
string(11) "/"
}
index.html
来自哪里?如何摆脱它?
答案 0 :(得分:1)
由于您的index.html
指令可能设置为DirectoryIndex
而导致DirectoryIndex index.html index.php
到来:
http://localhost/my-project
当您访问http://localhost/my-project/index.html
时,由于此指令,Apache加载REQUEST_URI
并且您的index.html
变为RewriteRule
(在当前目录中)。
当index.html
运行时,它只将当前URI作为GET参数传递,因此您在_url
GET参数中得到DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?_url=/$1 [QSA,L]
</IfModule>
。
您可以像这样调整规则:
http://localhost/my-project
现在,这个重写规则不会被.+
调用,因为它匹配{{1}}模式。