htaccess默认查询字符串值

时间:2015-01-30 23:21:22

标签: .htaccess

由于某些原因,我的.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来自哪里?如何摆脱它?

1 个答案:

答案 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}}模式。