为什么我得到一个301重定向到文件夹名以斜线?斜线、文件夹、重定向

2023-09-02 00:36:27 作者:热情喂给风

一个请求的http://本地主机/样品 - codeS /骨干网的mysql-读JSON /网站

被重定向如下:

 >请求的URL:http://本地主机/样品 -  codeS /骨干网的mysql-读JSON /网站
>请求方法:GET
>状态code:301永久移动
 

响应报头:

 > HTTP / 1.1 301永久移动日期:星期四,2012年9月6日14时32分41秒GMT
>服务器: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地点:
> HTTP://本地主机/样品 -  codeS /骨干网的mysql-读JSON /网站/
>内容长度:417保持活动:超时= 5,最大值= 100连接:
>保持活动的Content-Type:text / html的;字符集= ISO-8859-1
 
删除文件夹重定向

我不知道是什么原因导致这种重定向。

我在文件夹下面的.htaccess C: XAMPP htdocs中样品 - codeS 骨干-mysql的阅读,JSON

的.htaccess

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^的index.php [QSA,L]
 

解决方案

这是因为的 mod_dir和 DirectorySlash 指令是在做重定向。有了它,当Apache着眼于一个URI,并认为它是访问一个目录,而缺少结尾的斜线,其301重定向到URI与结尾的斜线。它总是默认打开的,因为如果你已经关闭了它有一个信息泄露的安全问题。但是,如果你通过一个的index.php脚本路由的一切,它甚至有可能不会不要紧,你可以通过简单地增加 DirectorySlash关在你的htaccess文件将其关闭(并把它对可以访问的目录,如CSS或JS或图像等目录。

A request to http://localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites

gets redirected as follows:

> Request URL:http://localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites
> Request Method:GET
> Status Code:301 Moved Permanently

Response headers:

> 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

I am not sure what causes this redirect.

I have the following .htaccess in folder c:xampphtdocsSAMPLE-CODESbackbone-mysql-reading-json:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

解决方案

This is probably because of the mod_dir and the DirectorySlash directive that's doing the redirect. With it on, when apache looks at a URI and thinks it's accessing a directory, and is missing the trailing slash, it 301 redirects to the URI with the trailing slash. It's always turned on by default because there's an information disclosure security issue if you have it turned off. But if you are routing everything through an index.php script, it may not even matter and you can turn it off by simply adding DirectorySlash Off in your htaccess file (and turn it on for directories that you can access directory, like css or js or images, etc.