只允许某些 IP 地址使用 mod_rewrite 访问站点? [英] Only allow certain IP addresses to access site with mod_rewrite?

查看:22
本文介绍了只允许某些 IP 地址使用 mod_rewrite 访问站点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的站点上有一个目录,我们只想通过几个 IP 地址访问该目录.所以我们有这个 .htaccess 文件来尝试让它工作:

We have a directory on our site which we only want to be accessible by a couple of IP addresses. So we have this .htaccess file to try and get it working:

RewriteEngine on
RewriteCond %(REMOTE_ADDR) !^123.123.123.123
RewriteCond %(REMOTE_ADDR) !^124.124.124.124
RewriteCond %{REMOTE_ADDR} !^125.125.125.125
RewriteCond %{REMOTE_ADDR} !^126.126.126.126
RewriteCond %{REMOTE_ADDR} !^127.127.127.127
RewriteCond %{REMOTE_ADDR} !^128.128.128.128

RewriteCond %{HTTP_REFERER} !^http://www.example.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/ [NC]

RewriteRule ^.*$ http://www.example.com [R=301,L]

我们想要它做的是,如果请求不是来自我们的 IP 地址之一(显然只是为了示例而在上面的代码中更改了它们),或者引用者不是来自特定站点,然后重定向他们在别处.

What we want it to do is, if the request does NOT come from one of our IP addresses (obviously changed them in the code above just for examples sake), or the referrer is not coming from a specific site, then redirect them elsewhere.

出于某种奇怪的原因,它适用于位于 126.126.126.126 位置的 IP,但不适用于其他 IP.真的无法弄清楚为什么它会根据IP而有所不同.可能出什么问题了?

For some bizarre reason, it works for the IP which is in the position of the 126.126.126.126 one, but doesn't work for the others. Really can't figure out why it would be any different depending on the IP. What could be going wrong?

我知道这会带来安全问题,因为推荐人可能会被欺骗,但我们很快就会提出更好的解决方案,这只是临时措施.

I'm aware this poses a security issue as the referrer can be spoofed, but we'll be coming up with a better solution soon, this is just a temporary measure.

推荐答案

忘记发布这个问题的答案.最后只是打字错误..DOH!

Forgot to post the answer to this. It was just a typo in the end.. DOH!

如果您注意到,有些 %{REMOTE_ADDR} 行有大括号,有些有普通括号!他们都需要卷曲的.

If you notice, some of the %{REMOTE_ADDR} lines have curly brackets, and some have normal brackets! They all needed curly ones.

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteCond %{REMOTE_ADDR} !^124.124.124.124
RewriteCond %{REMOTE_ADDR} !^125.125.125.125
RewriteCond %{REMOTE_ADDR} !^126.126.126.126
RewriteCond %{REMOTE_ADDR} !^127.127.127.127
RewriteCond %{REMOTE_ADDR} !^128.128.128.128

RewriteCond %{HTTP_REFERER} !^http://www.example.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com/ [NC]

RewriteRule ^.*$ http://www.example.com [R=301,L]

这篇关于只允许某些 IP 地址使用 mod_rewrite 访问站点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆