通用mod_rewrite引用者检查

tyl*_*erl 7 apache .htaccess mod-rewrite

我正在寻找一个通用的(独立于主机的)mod_rewrite规则集,用于对资源进行HTTP_REFERER检查.我提出了以下似乎很直观的内容,但遗憾的是它不起作用:

RewriteCond %{HTTP_REFERER} !^https?://%{HTTP_HOST}/.*
# RewriteRule .* - [F]  # <- or whatever
Run Code Online (Sandbox Code Playgroud)

显然你不能在比较的两边都有变量.所以,一个黑客:

RewriteCond %{HTTP_HOST}##%{HTTP_REFERER} !^([^#]*)##https?://\1/.*
Run Code Online (Sandbox Code Playgroud)

但哇,这很难看 - 如果你不确切知道发生了什么,那就非常混乱了.

是否有更好(更清洁)的方式来编写这些规则?

Ter*_*ryE 6

"如果你不确切知道发生了什么,那就非常令人困惑"

首先祝贺解决方法.检查源,mod_rewrite.c似乎没有做任何形式的变量插值,所以我想不出另一种选择.至于你的"令人困惑"的一点,这不是我们有评论的原因吗?我也整理了你的正则表达式(例如尾随.*是多余的)并使用=作为一个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/
Run Code Online (Sandbox Code Playgroud)