Apache 通用mod_重写参考者检查

Apache 通用mod_重写参考者检查,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我正在寻找一组通用(独立于主机)的mod_重写规则,用于对资源执行HTTP_REFERER检查。我提出了以下看似直观,但令人遗憾的是不起作用的观点: RewriteCond %{HTTP_REFERER} !^https?://%{HTTP_HOST}/.* # RewriteRule .* - [F] # <- or whatever 但是哇,这太难看了——如果你不知道到底发生了什么,那就太令人困惑了 有没有更好(更干净)的方法来编写这些规则 “如果你不知道到底发生了什么,那就太令人

我正在寻找一组通用(独立于主机)的mod_重写规则,用于对资源执行HTTP_REFERER检查。我提出了以下看似直观,但令人遗憾的是不起作用的观点:

RewriteCond %{HTTP_REFERER} !^https?://%{HTTP_HOST}/.*
# RewriteRule .* - [F]  # <- or whatever
但是哇,这太难看了——如果你不知道到底发生了什么,那就太令人困惑了

有没有更好(更干净)的方法来编写这些规则

“如果你不知道到底发生了什么,那就太令人困惑了”

首先恭喜你找到了解决办法。检查源代码,mod_rewrite.c似乎不做任何形式的变量插值,所以我想不出替代方法。至于你“令人困惑”的观点,这不是我们发表评论的原因吗?我还整理了您的regexp(例如,尾部的。*是多余的),并将=用作delim来强调您正在进行比较

它可能看起来很俗气,但您的想法在运行时方面几乎是最优的

#
# Is **HTTP_REFERER** of the form http(s)://HTTP_HOST/....
# Note that mod_rewrite only does interpolation in the teststring so this is
# set up in the format AAAA=BBBB and the pattern uses a backreference (\1) to
# match the corresponding elements of AAAA and BBBB
#
RewriteCond %{HTTP_HOST}==%{HTTP_REFERER} !^(.*?)==https?://\1/

用rewriteCond做这件事似乎很复杂。也许mod_宏是另一种思考主机无关规则问题的方式。你们都是天才。非常感谢。
#
# Is **HTTP_REFERER** of the form http(s)://HTTP_HOST/....
# Note that mod_rewrite only does interpolation in the teststring so this is
# set up in the format AAAA=BBBB and the pattern uses a backreference (\1) to
# match the corresponding elements of AAAA and BBBB
#
RewriteCond %{HTTP_HOST}==%{HTTP_REFERER} !^(.*?)==https?://\1/