我有一个.htaccess文件,目前看起来像:
<Files .htaccess> order allow,deny deny from all </Files> Options All -Indexes IndexIgnore * # Respond to /include/ with 404 instead of 403 RewriteEngine On RedirectMatch 404 ^/include(/?|/.*)$
我试图阻止每个人都知道/ include / exists,但是我遇到了一个问题。
虽然访问http://www.example.com/include给出了404,但浏览器会添加一个结束斜杠(因此给出http://www.example.com/include/),表明该目录实际上在那里,但正在伪装。访问实际的404时,不会附加结束斜杠。
有没有办法防止这种行为?
答案 0 :(得分:3)
1。您可以使用DirectorySlash Off
告诉Apache不要在目录末尾添加尾部斜杠。
2. 如果您实际上没有使用重写引擎(基于您提供的代码),为什么要使用RewriteEngine On
? RedirectMatch
与mod_rewrite
无关。
3。如果您想在此处使用 mod_rewrite ,请尝试此规则 - 它将返回/include
文件夹的404代码(包含和不包含尾随)斜杠)以及该文件中的任何资源(例如/include/main.php
等)。
RewriteEngine On
RewriteRule ^include(/|/.+)?$ - [R=404,L]
答案 1 :(得分:0)
除RedirectMatch外还使用重写规则。
答案 2 :(得分:0)
在某些情况下,以下代码行不足以隐藏目录:
RewriteEngine On
RewriteRule ^include(/|/.+)?$ - [R=404,L]
这些行不会阻止诸如include/.php
,include/.ht
,include/.htaccess
之类的请求(相反,您将收到403 Forbidden
错误,该错误指示目录{{1} })。相反,有人希望禁止访问所有 dot-files 以防止文件夹泄漏(include
,.git
(只是“ .php”-没有文件名),{ {1}},...)。 (<旁注:我猜.htaccess文件和主Apache配置文件之间可能存在一些问题,您可以在其中禁止访问“ .ht”文件并允许执行“ .php”文件-这就是为什么这样的请求在做自己的事情的时候却没有简单地重写为404错误。相反,它们返回了403错误)。
因此您可以通过发送以下请求来绕过上述规则(您将收到.php
错误):
如果目录不存在,您可以验证得到.htaccess
错误:
您需要匹配所有点文件。因此,首先您禁止访问它们(403错误),然后将403错误重写为404错误。
403 Forbidden
现在这些请求已被阻止,您将收到一个404 Not Found
错误:
这是我类似问题的答案:Rewrite requests to directory with htaccess to 404 error