Apache DirectorySlash Off - 站点中断 [英] Apache DirectorySlash Off - Site breaks
问题描述
如果我在 .htaccess
文件中设置 DirectorySlash Off
并调用目录而没有尾部斜杠,我会得到一个 403-从我的服务器禁止
.如果我用斜杠调用它,一切正常.
If i set DirectorySlash Off
in my .htaccess
file and call the directory without the trailing slash i get an 403-Forbidden
from my server. If i call it with slash everything works fine.
谁能解释一下为什么?这是我完全匿名的 .htaccess
:
Could anyone explain why? Here are my fully anonymized .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]
推荐答案
如您所知,根据文档,当 DirectorySlash
设置为 Off
时,请求 /folder
没有评估 DirectoryIndex
.这意味着请求不会自动映射到 /folder/index.php
.
As you know per the documentation, when DirectorySlash
is set to Off
, requests to /folder
do not have DirectoryIndex
evaluated. This means that the request will not be automatically mapped to /folder/index.php
.
mod_dir
在请求处理的修复"阶段执行此检查.mod_rewrite
负责您的 RewriteRule
定义,当您在 .htaccess
文件中指定规则时,它也会在此阶段执行其处理.
mod_dir
performs this check in the "fixup" phase of the request processing. mod_rewrite
, which is responsible for your RewriteRule
definitions, also performs its processing in this phase when you specify the rules in a .htaccess
file.
然而,它的编程考虑了像 mod_dir
这样的模块,并包括一个检查以确保当前目录是用尾部斜杠请求的.如果没有,它拒绝处理请求,因为这样做可能会导致未定义的行为.
However, it was programmed with an awareness of modules like mod_dir
, and includes a check to make sure that the current directory was requested with a trailing slash. If not, it declines to handle the request, since doing so might lead to undefined behaviour.
然后请求进入内容生成阶段,由于请求未映射到真实文件,因此由 mod_autoindex
处理.鉴于您的主机默认禁用 Indexes
,mod_autoindex
返回 403 Forbidden
,这就是您所看到的.
The request then moves on to the content-generation phase, which, since the request was not mapped to a real file, is handled by mod_autoindex
. Given that Indexes
are disabled on your host by default, mod_autoindex
returns 403 Forbidden
which is what you see.
请注意,由于 DirectoryIndex
未被评估,即使 mod_rewrite
被 处理请求,它仍然会失败,因为没有自动index.php
的解析会发生,你的规则
Note that since DirectoryIndex
is not evaluated, even if mod_rewrite
were to process the request, it would still fail, because no auto-resolution to index.php
would occur, and your rule
RewriteRule . /folder/index.php [L]
不会匹配,因为 .
需要匹配某些内容(但请求将为空).
wouldn't match, because the .
requires a match on something (but the request would be blank).
启用 DirectorySlash
通过更正前面提到的所有场景中被阻止的操作来防止这种情况,最后一个注释除外,这是由 DirectoryIndex
映射的事实处理的对 index.php
的请求.
Enabling DirectorySlash
prevents this scenario by correcting the prevented actions in all of the previously mentioned scenarios except the last note, which is taken care of by the fact that DirectoryIndex
maps the request to index.php
anyway.
这篇关于Apache DirectorySlash Off - 站点中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!