将 403 Forbidden 重定向到 404 Not Found 的问题 [英] Problem redirecting 403 Forbidden to 404 Not Found
问题描述
我的 .htaccess 的相关部分如下所示:
The pertinent part of my .htaccess looks like this:
Options -Indexes
<FilesMatch include>
Order allow,deny
Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/.*)$
它会生成以下响应:
- /包含 403
- /include/404
- /include/config.inc 403
通过查看我的模式,我可以看出问题可能在 (/.*) 部分,但我尝试过的所有方法都给出了相同的结果;我没有一直得到 404,而是在一种情况下得到 404,对其他所有情况得到 403.我使用的表达方式有什么问题?或者,由于我必须对几个目录执行此操作,是否有一种全面的方法可以让我将所有 403 响应转换为 404?
I can tell by looking at my pattern that problem is likely in the (/.*) part but everything I have tried gives me the same results; instead of consistently getting 404 I get a 404 for the one case and 403 for everything else. What is wrong with the expression I'm using? Alternatively since I have to do this for a few directories is there a blanket approach that would allow me to convert all 403 responses to 404?
更新:我发现通过删除 FileMatch 可以获得更好的结果,所以我的 .htaccess 现在看起来像这样:
UPDATE: I've found that by removing the FileMatch I get better results, so my .htaccess now looks like this:
Options -Indexes
RedirectMatch 404 ^/include(/.*)?$ # Added dlamblin's first suggestion
并生成以下响应:
- /包含 404
- /include/404
- /include/config.inc 403
更新:有趣的是,我发现以下会产生不同的输出:
UPDATE: Interestingly enough I have discovered that the following produces different output:
RedirectMatch 404 ^/include(/?|/.*)$
RedirectMatch 404 ^/template(/?|/.*)$
模板模式适用于所有情况,但是包含仍然为包含中的所有文件生成 403(例如/include/config.inc)这可能是目录名称的问题而不是 .htaccess 的问题文件本身?
The template pattern works on all cases however include is still generating 403 for all files in include (e.g. /include/config.inc) Could this be an issue with the directory name and not a problem with the .htaccess file itself?
更新:我的 .htaccess 中的以下内容在访问/include/config.inc 时与重定向冲突.
UPDATE: The following in my .htaccess was conflicting with redirect when accessing /include/config.inc.
<FilesMatch config>
Order allow,deny
Deny from all
</FilesMatch>
推荐答案
我可以理解为什么/include 没有被 RedirectMatch 捕获,您没有将结尾 '/' 设为可选,但是/include/config.inc 部分有点令人费解.
I can understand why the /include isn't caught by your RedirectMatch, you aren't making the end '/' optional, however the /include/config.inc part is a bit on the puzzling side.
这是我在 Apache 2.2 上的工作:
Here is what I got to work on Apache 2.2:
<FilesMatch /include(/?|/.*)>
Order allow,deny
Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/?|/.*)$
这处理这些情况:
/include 404
/include/ 404
/include/config.inc 404
我必须更改 FilesMatch 部分才能使/include 部分正常工作.
I had to change the FilesMatch part in order for the /include part to work properly.
匹配行在没有 <FilesMatch> 的情况下也能工作..htaccess 中的部分并给出预期结果.
The match line also works without the <FilesMatch> section in .htaccess and gives the expected results.
这篇关于将 403 Forbidden 重定向到 404 Not Found 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!