.htaccess 当用户请求文件夹时,如何防止301重定向?

.htaccess 当用户请求文件夹时,如何防止301重定向?,.htaccess,mod-rewrite,redirect,.htaccess,Mod Rewrite,Redirect,我一直在尝试制作一个忽略现有目录的前端控制器。在某些情况下,我已经得到301重定向,我希望不会发生。我正在使用以下.htaccess文件: DirectoryIndex index.php Options -Indexes RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ index.php?action=$1 [QSA,L] 在我一直在测试的目录中,我有以下设置: test/ .htaccess i

我一直在尝试制作一个忽略现有目录的前端控制器。在某些情况下,我已经得到301重定向,我希望不会发生。我正在使用以下
.htaccess
文件:

DirectoryIndex index.php
Options -Indexes

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?action=$1 [QSA,L]
在我一直在测试的目录中,我有以下设置:

test/
.htaccess
index.php
如果我尝试查询服务器,我的第二个示例将导致301重定向:

curl -s server/tes    | Outputs: tes
curl -s server/test   | 301 Redirect; on follow will output: test
curl -s server/test/  | Outputs: test/

如何防止在访问
服务器/测试时发生301?我觉得我遗漏了一些非常琐碎的东西。

这是因为
test
是一个目录,
mod_dir
模块在
mod_rewrite
之后运行,添加了一个尾随斜杠并执行301重定向。您可以通过在.htaccess顶部添加此行来防止它:

DirectorySlash Off
但是请记住,它被认为是一种安全风险,因为它可以显示目录列表

您可以使用以下规则在htaccess自身中进行重定向:

# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]

正是我要找的!在我不介意安全风险的情况下(仅在本地使用),我仍然对安全影响感到好奇。我注意到当
mod_autoindex
处于活动状态(
Options+index
)时提到的内容。既然我禁用了这个功能,
选项-索引
,我会好吗?是的,我认为使用
选项-索引
应该可以。