【问题标题】:htaccess Customized 403 Forbidden page not always workinghtaccess 自定义 403 禁止页面并不总是有效
【发布时间】:2021-03-22 20:11:06
【问题描述】:

我有一个自定义 403 页面,当我想阻止特定页面时它可以工作,但当我想匹配特定 HTTP_REFERER 时它不起作用。

使用特定的 HTTP_REFERER,我得到了常规的 403,为了测试 HTTP_REFERER,我在另一个站点“mysite.com”上添加了一个指向该项目的链接,当我点击该链接时,我得到了服务器 403 响应:

但是我打开了我的测试页面“forbidden-test”,我确实得到了我自定义的forbidden.php页面

这是我在 htaccess 中的内容,以下示例仅通过显示我的自定义 403 页面 RewriteRule ^forbidden-test$ - [F] 起作用:

Options All -Indexes
# prevent folder listing
IndexIgnore *
RewriteEngine on

RewriteCond %{HTTP_REFERER} \
...  (there are many here)
mysite\.com|\
[L,NC]
RewriteRule .* - [F]
#spam blacklist end

RewriteCond %{HTTP_USER_AGENT} \
12soso|\
192\.comagent|\
1noonbot|\
1on1searchbot|\
3de\_search2|\
3d\_search|\
3g\ bot|\
...  (there are many here)
zyte\
[NC]
RewriteRule .* - [F]
#bad bots end


#Forbidden Test
RewriteRule ^forbidden-test$ - [F]

#ERRORS
RewriteRule ^forbidden/?$ forbidden.php [L,QSA]

ErrorDocument 403 /forbidden

有什么想法吗? 谢谢

【问题讨论】:

  • 尝试将 ErrorDocument 403 /forbidden 放在 .htaccess 文件的顶部
  • @anubhava 相同的结果 :( 谢谢
  • RewriteCond %{HTTP_REFERER} \ ... (这里有很多) mysite\.com|\ [L,NC], L 可能会出错。
  • 感谢@jacouh,为简单起见,我将它们全部删除,但它们都在工作,我什至只用一个 (mysite.com) 测试了代码并得到了相同的结果,感谢您的输入。

标签: .htaccess http-status-code-403 custom-error-pages


【解决方案1】:

注意默认错误消息的实际含义:

此外,在尝试使用 ErrorDocument 处理请求时遇到 403 Forbidden 错误。

对您的自定义错误文档的访问被阻止。内部请求再次进行所有重写;并且由于原始请求的引用者(仍然)错误,因此现在禁止访问您的 403 文档

您需要为此引用者检查添加一个例外,以便它允许访问您的错误文档。

这里最简单的方法可能是在请求此特定文档时将规则放在最顶部,什么都不做:

RewriteRule ^forbidden\.php$ - [L]

- 是“不可重写”的,它什么也不做。 [L] 标志在这里很重要,表示“在这一轮中不要处理任何其他规则。”

另外,由于你的错误文档似乎是一个 PHP 脚本,你应该直接这样定义它,

ErrorDocument 403 /forbidden.php

否则,这需要额外的一轮重写,从 /forbidden 到 /forbidden.php,真的没有什么好的理由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2014-12-19
    • 2014-10-22
    • 2012-07-04
    • 1970-01-01
    • 2014-12-01
    • 2017-05-16
    相关资源
    最近更新 更多