htaccess 默认查询字符串值

htaccess Default Query String Value

出于某种原因,我的 .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=/ [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 来自哪里,我该如何摆脱它?

index.html 由于您的 DirectoryIndex 指令可能设置为:

DirectoryIndex index.html index.php

当您访问 http://localhost/my-project 时,Apache 加载 http://localhost/my-project/index.html 由于此指令,您的 REQUEST_URI 变为 index.html(在当前目录中)。

RewriteRule 运行时,它只是将当前 URI 作为 GET 参数传递,因此您在 _url GET 参数中得到 index.html

您可以像这样调整您的规则:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ index.php?_url=/ [QSA,L]
</IfModule>

现在不会为 http://localhost/my-project 调用此重写规则,因为它匹配 .+ 模式。