我正在寻找一组通用的(独立于主机的)mod_rewrite 规则,用于对资源进行 HTTP_REFERER 检查。我想出了以下看起来很直观,但遗憾的是不起作用:
RewriteCond %{HTTP_REFERER} !^https?://%{HTTP_HOST}/.*
# RewriteRule .* - [F] # <- or whatever
显然你不能在比较的两边都有一个变量。所以,一个黑客:
RewriteCond %{HTTP_HOST}##%{HTTP_REFERER} !^([^#]*)##https?://\1/.*
但是哇,这太丑了——如果你不知道到底发生了什么,那就太令人困惑了。
有没有更好(更干净)的方式来编写这些规则?
最佳答案
"if you don't know exactly what's going on, it's terribly confusing"
首先祝贺解决方法。检查源代码,mod_rewrite.c 似乎没有做任何形式的变量插值,所以我想不出替代方案。至于你的“混淆”点,这不是我们有意见的原因吗?我还整理了您的正则表达式(例如,尾随 .* 是多余的)并使用 = 作为分隔符来强调您正在进行比较。
它可能看起来很俗气,但您的想法在运行时方面接近最佳。
#
# 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/
关于apache - 通用 mod_rewrite 引用检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7398191/