为什么我会收到带有斜杠的 301 重定向到文件夹名称?

tuc*_*son 4 .htaccess redirect

一个请求 http://localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites

被重定向如下:

> Request URL:http://localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites
> Request Method:GET
> Status Code:301 Moved Permanently
Run Code Online (Sandbox Code Playgroud)

响应头:

> HTTP/1.1 301 Moved Permanently Date: Thu, 06 Sep 2012 14:32:41 GMT
> Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4
> mod_perl/2.0.4 Perl/v5.10.1 Location:
> http://localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/
> Content-Length: 417 Keep-Alive: timeout=5, max=100 Connection:
> Keep-Alive Content-Type: text/html; charset=iso-8859-1
Run Code Online (Sandbox Code Playgroud)

我不确定是什么导致了这种重定向。

我在文件夹中有以下 .htaccess c:\xampp\htdocs\SAMPLE-CODES\backbone-mysql-reading-json\

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lin 6

这可能是因为mod_dir 和DirectorySlash执行重定向指令。启用它后,当 apache 查看一个 URI 并认为它正在访问一个目录,并且缺少尾部斜杠时,它 301 重定向到带有尾部斜杠的 URI。默认情况下它始终处于打开状态,因为如果将其关闭,则会出现信息泄露安全问题。但是,如果您通过 index.php 脚本路由所有内容,它甚至可能无关紧要,您可以通过简单地添加DirectorySlash Offhtaccess 文件将其关闭(并为您可以访问目录的目录打开它,例如 css 或 js 或图像, 等等。