【问题标题】:prevent directory access using setenvif使用 setenvif 防止目录访问
【发布时间】:2014-02-11 05:14:16
【问题描述】:

我想使用SetEnvIf 来阻止对特定 IP 地址的目录访问。
这是我想出的。

<Directory /main>
order allow,deny
SetEnvIf Remote_Addr ^(2|5|6)\. banned [OR]
SetEnvIf Remote_Addr ^(7|8|9)\. banned
allow from all
deny from env=banned
</Directory>

(2|5|6)\.(7|8|9)\. 是通配符 IP 地址示例,
我试图阻止这些范围访问我服务器上的main 目录。
但不确定 [OR] 和通配符 IP 地址是否有效。
另外我如何将banned 重定向到http://officeofstrategicinfluence.com/spam/
而不是仅仅拒绝或阻止他们?

【问题讨论】:

标签: apache .htaccess


【解决方案1】:

[OR] 不能像您使用的那样使用,但可以按照正则表达式语法使用。试试这个代码:

<Directory /main>
SetEnvIf Remote_Addr ^(2|5|6|7|8|9)\. banned

order allow,deny
allow from all
deny from env=banned
</Directory>

另请注意,&lt;Directory&gt; 指令仅适用于 Apache 配置,不适用于 .htaccess

更新:根据 cmets,您可以在根 .htaccess 中使用此重写规则:

RewriteEngine On

RewriteCond %{REMOTE_ADDR} ^(2|5|6|7|8|9)\.
RewriteRule ^main(/|$) http://officeofstrategicinfluence.com/spam/ [NC,L,R]

【讨论】:

  • 对于htaccess 使用&lt;directory&gt; 是否有替代方法?有没有办法用它强制重定向而不是仅仅拒绝?
  • 我认为我无法访问我的 http_config 文件以用于 RewriteMap,只有 htaccess。也很困惑如何将http://officeofstrategicinfluence.com/spam/ 的重定向添加到您的代码中。
  • 我什至没有在我的回答中提到RewriteMap。并澄清你想用http://officeofstrategicinfluence.com/spam/做什么?是否要将/main/ 重定向到此网址?
  • 澄清我想要做什么,对于匹配(2|5|6|7|8|9)\.的任何IP地址,当他们尝试转到/main/ 时,他们会被重定向到`officeofstrategicinfluence.com/spam'。我还与主机核实过,他们说如果需要,RewriteMap 已启用。
  • 好吧,我提到 RewriteMap 的原因是因为我在查找 ipmap: 时引用了它,并且不确定它是否与代码相关。
猜你喜欢
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多