如何使用 htaccess 文件重定向或拒绝来自特定国家/地区的用户?

发布于 2024-12-07 04:10:52 字数 717 浏览 1 评论 0原文

我查看了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2024-12-14 04:10:52

位于被 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 like

ErrorDocument 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文