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

查看:149
本文介绍了只允许某些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天全站免登陆