2010-07-15 33 views
6

如果我在.htaccess文件中设置DirectorySlash Off并调用目录结尾的斜线我从服务器获取的403-Forbidden。如果我用斜线调用它,一切正常。阿帕奇DirectorySlash关 - 网站打破

任何人都可以解释为什么吗?下面是我的完全匿名.htaccess

# GLOBAL CONFIG 
Options +FollowSymlinks 
DirectorySlash Off 
AddDefaultCharset utf-8 
php_value post_max_size 256M 
php_value upload_max_filesize 256M 

# BEGIN WordPress 
RewriteEngine On 
RewriteBase /folder/ 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /folder/index.php [L] 
# END WordPress 

# REMOVE WWW 
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L] 

回答

11

当你每文档知道,当DirectorySlash设置为Off,请求/folder没有DirectoryIndex评估。这意味着请求不会自动映射到/folder/index.php

mod_dir在请求处理的“修正”阶段执行此项检查。 mod_rewrite负责您的RewriteRule定义,当您在.htaccess文件中指定规则时,还会在此阶段执行处理。

然而,它被编程为模块的知名度,如mod_dir,并且包含一个检查以确保当前目录被要求用尾部斜线。如果不是,它拒绝处理请求,因为这样做可能会导致未定义的行为。

该请求然后转移到内容生成阶段,由于请求未映射到真实文件,因此该请求由mod_autoindex处理。鉴于Indexes在您的主机上默认为禁用,mod_autoindex返回403 Forbidden这就是你所看到的。

注意,因为DirectoryIndex不计算,即使mod_rewrite来处理请求时,它仍然会失败,因为没有自动分辨率index.php会发生,您的规则

RewriteRule . /folder/index.php [L] 

止跌” t匹配,因为.需要匹配某项内容(但请求将为空)。

启用DirectorySlash通过纠正前面提到的所有场景的阻止行动,除了最后一个音符,这是由DirectoryIndex请求index.php映射反正事实照顾防止这种情况。

+0

非常感谢!如果我阅读你的答案,也容易理解。 – gearsdigital 2010-07-18 05:03:18

+0

但是如果我们想要混淆我们的目录结构并且仍然使用'mod_rewrite'来处理每个请求呢?假设我有一个拥有'/ private'目录的网站。如果'DirectorySlash'为'On',如果我请求'/ private',Apache会将我重定向到'/ private /',从而让信息泄露进来。如果我只想发送'/ private'到我的'索引处怎么办? PHP的前端控制器? – tonix 2016-05-08 17:22:28

2

我想是因为当你打开DirectorySlash关闭,它禁用URL的自动校正,并试图显示目录列表中,但幸运的是,你可能已经停用了这个地方(或文件权限),所以它发送一个403-Forbidden。我想,当你打开它,它正常工作。 从我从文档中了解的情况来看,使用DirectorySlash关闭安全性并不是很好。 http://httpd.apache.org/docs/2.1/mod/mod_dir.html

+0

洛朗,感谢您的回答。我已经阅读了apache文档中的**安全警告**,但我需要了解这里出了什么问题。 顺便说一下,在我的虚拟主机后端默认禁用了目录列表。 – gearsdigital 2010-07-15 19:25:56

+0

你'rewriteBase是/ folder /',所以你需要结尾的斜线来匹配url。你可以将它设置为'/ folder',我认为它应该没有结尾的斜杠。 – laurent 2010-07-15 19:35:45

+0

如果我按照你的建议,我也会得到403。 – gearsdigital 2010-07-15 20:06:29

8

使用Apache 2.4,您可以通过设置RewriteOptions AllowNoSlash来允许在.htaccess文件中进行重写。

Changes with Apache 2.3.16 
... 
*) mod_rewrite: Add the AllowNoSlash RewriteOption, which makes it possible 
    for RewriteRules to be placed in .htaccess files that match the directory 
    with no trailing slash. PR 48304. 
    [Matthew Byng-Maddick <matthew byng-maddick bbc.co.uk>] 
... 

Apache documentation of mod_rewrite