apache - 通用 mod_rewrite 引用检查

标签 apache .htaccess mod-rewrite

我正在寻找一组通用的(独立于主机的)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/

相关文章:

apache - Options +Indexes 不会显示文件名

.htaccess - htaccess 仅当 HTTP 引荐来源网址不等于某些内容时才重定向

wordpress - htaccess 不重定向并显示 404

.htaccess - 使用 SSL 重写规则不起作用

apache - .htaccess 将非 www 重定向到 www,但一个目录不起作用

apache - 如何将 Apache 文档根目录指向 Ubuntu 中的特定文件夹?

apache - 使用 Google Drive Directory 作为 Apache 虚拟主机

python - 永远运行服务器端脚本的方法有哪些?

.htaccess 子域的通配符 SSL

apache - htaccess - 如果末尾有问号,则剪掉问号