如何使用 htaccess 文件重定向或拒绝来自特定国家/地区的用户?
我查看了 countryipblocks.net,需要澄清...
如果我想阻止用户访问,例如安道尔我的网站,到底需要添加什么到我的(已经存在的).htaccess 文件中?
我是否需要简单地将这段文本添加到我的 .htaccess 中?
<Limit GET HEAD POST>
order allow,deny
deny from 85.94.160.0/19
deny from 91.187.64.0/19
deny from 194.117.123.178/32
deny from 194.158.64.0/19
deny from 195.112.181.196/32
deny from 195.112.181.247/32
allow from all
</LIMIT>
另一方面,如果我想将来自克罗地亚的用户从 http://mywebsite.com 重定向到 < a href="http://google.com" rel="nofollow">http://google.com 或目标网页,究竟需要将什么添加到我的 .htaccess 文件中?
最后 - 对于被拒绝访问的用户来说,“拒绝”会如何出现?
谢谢。
I looked at countryipblocks.net, and need to clarify...
If I want to block users from, say, Andorra from visiting my site, what exactly needs to be added to my (already existing) .htaccess file?
Do I need to simply add this block of text to my .htaccess?
<Limit GET HEAD POST>
order allow,deny
deny from 85.94.160.0/19
deny from 91.187.64.0/19
deny from 194.117.123.178/32
deny from 194.158.64.0/19
deny from 195.112.181.196/32
deny from 195.112.181.247/32
allow from all
</LIMIT>
On the other hand, if I want to redirect users from, say, Croatia, from http://mywebsite.com to http://google.com or a landing page, what exactly needs to be added to my .htaccess file?
Finally - how would "deny" appear to the user being denied access?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
位于被
deny
禁止的 IP 范围内的访问者将收到 403 错误。 需要类似ErrorDocument 403 /errors/403.html
如果您希望他们看到一个漂亮的页面,而不是标准的 Apache 错误,那么您的 .htaccess 文件中 的内容。通过将阻止的 IP 设置为 127.0.0.1(ie.localhost),可以很容易地检查基于 IP 地址的规则在 .htaccess 中是否有效;然后,当您在本地主机上查看相关页面时,您应该会看到该页面被阻止的结果。
在回答您有关重定向用户的问题时,阻止来自任何 1 个国家/地区的所有用户似乎有点矫枉过正;但是,请尝试阅读
RewriteCond
指令。Visitors who are within a IP range that is banned by
deny
will be served with a 403 error. If you want to them to see a nice page, instead of the standard Apache error, then you will need something likeErrorDocument 403 /errors/403.html
in your .htaccess file. It is fairly easy to check rules based on IP addresses are working in your .htaccess by setting the blocked IP to be 127.0.0.1 (i.e. localhost); when you then look at the page in question on localhost, you should see the result of the page being blocked.
In answer to your question about redirecting users, blocking all users from any 1 country seems a little bit overkill; however, try reading up on the
RewriteCond
directive.