PHP

.htaccess 封禁中国IP

首先强调一下这与爱国无关。
出于不同原因,很多站长需要封禁中国ip,特别是做英文站的朋友。因为对于Google AdSense来说,中文ip是低价的代名词。
屏蔽ip段很简单,在.htaccess里加入:
order allow,deny
# 禁止一个ip地址
deny from 123.125.125.147
# 禁止一个ip段
deny from 210.145.95.0/255
# 或者你需要在这ip段中允许部分ip
allow from 210.145.95.123
# 除此之外一切均可以访问
allow from all
了解了.htacess的deny ip用法后,我们只需要有中国ip就能解决问题了。上Google找了一圈发现这个叫做 <a href=”http://www.parkansky.com/china.htm” target=”_blank”>Stop Traffic From China IP Address Blocks To Protect Your Web Server From Chinese Hackers</a>的文章,其文章已经通过.htaccess对中国ip实行封禁,所以要访问的话得翻墙了(推荐一个<a href=”http://www.freedaili.com/” target=”_blank”>在线代理</a>)。这篇文章的初衷在于限制国内ip来防止中国黑客攻击,看来中国黑客还是比较有名的啊,同样有名的还有俄罗斯的,呵呵。
文章给出了.htaccess全文来限制中国及中国周边国家的ip,文中将台湾写成中国的邻国,俺改了一下下:) (中国ip中最后三个ip段为台湾省ip):
# This blocklist was last updated on Saturday, 17-May-2008 09:43:34 MDT
# 最新更新时间:2008-05-17
order deny,allow
# Chinese IP addresses follow:
# 中国ip地址
deny from 58.22.0.0/15 58.56.0.0/15 58.58.0.0/16 58.59.0.0/17 58.60.0.0/14 58.82.0.0/15 58.208.0.0/12 58.246.0.0/15 58.248.0.0/13 59.32.0.0/13 59.40.0.0/15 59.42.0.0/16 59.56.0.0/13 60.12.0.0/16 60.28.0.0/15 60.160.0.0/11 60.194.0.0/15 60.208.0.0/13 60.216.0.0/15 61.4.64.0/20 61.48.0.0/13 61.128.0.0/10 61.135.0.0/16 61.145.73.208/28 61.179.0.0/16 61.183.0.0/16 61.184.0.0/16 61.185.219.232/29 61.188.0.0/16 61.232.0.0/14 61.236.0.0/15 118.132.0.0/14 119.18.192.0/20 121.32.0.0/14 123.4.0.0/14 124.42.64.0/18 124.236.0.0/14 125.40.0.0/13 159.226.0.0/16 202.66.0.0/16 202.96.0.0/12 202.96.128.0/18 202.108.0.0/16 203.69.0.0/16 203.169.160.0/19 210.5.0.0/19 210.14.128.0/19 210.21.0.0/16 210.51.0.0/16 210.192.96.0/19 211.76.96.0/20 211.78.208.0/20 211.90.0.0/15 211.136.0.0/13 211.233.70.0/24 211.144.12.0/22 211.144.160.0/20 211.152.14.0/24 211.154.128.0/19 211.157.32.0/19 211.161.24.128/26 218.0.0.0/11 218.56.0.0/13 218.64.0.0/11 218.88.0.0/13 218.96.0.0/14 218.102.0.0/16 218.104.136.128/25 218.242.0.0/16 219.128.0.0/11 219.232.0.0/19 220.160.0.0/11 220.181.0.0/16 220.192.0.0/12 220.228.70.0/24 220.248.0.0/14 220.250.0.0/19 220.252.0.0/16 221.10.0.0/16 221.11.0.0/16 221.192.0.0/14 221.208.0.0/14 221.212.0.0/16 221.216.0.0/13 221.224.0.0/13 221.228.0.0/14 221.238.0.0/15 222.32.0.0/11 222.76.0.0/14 222.80.0.0/12 222.136.0.0/13 222.166.0.0/16 222.168.0.0/15 222.172.222.0/24 222.184.0.0/13
# Japan (hacking, scraping, or spamming)
deny from 218.225.179.0/24
# Korea IP addresses follow:
# 韩国ip
deny from 58.72.0.0/13 58.239.0.0/16 58.140.0.0/14 59.0.0.0/11 59.86.192.0/18 59.186.0.0/15 61.96.0.0/12 61.248.0.0/13 116.120.0.0/13 118.32.0.0/11 118.128.0.0/14 121.128.0.0/10 122.99.128.0/17 123.111.48.0/20 124.0.0.0/15 124.50.87.161 125.128.0.0/11 125.176.0.0/12 125.240.0.0/13 143.248.0.0/16 202.179.176.0/21 210.93.0.0/16 210.94.0.0/15 210.118.216.192/26 210.205.219.0/24 211.32.0.0/12 211.48.0.0/15 211.104.0.0/13 211.112.0.0/13 211.168.0.0/14 211.172.0.0/14 211.176.0.0/12 211.192.0.0/13 211.211.36.0/23 211.232.0.0/13 211.240.0.0/12 218.38.34.0/24 218.144.138.0/26 218.147.0.0/16 219.240.0.0/15 219.248.0.0/13 219.250.88.0/21 220.73.22.160/27 220.95.88.0/24 221.128.0.0/12 221.144.0.0/12 221.160.0.0/13 221.168.0.0/16 221.163.46.0/24 222.96.0.0/12 222.112.0.0/13 222.120.0.0/15 222.122.0.0/16 59.124.0.0/14 203.71.0.0/16 203.72.0.0/16
# Neighboring Asian countries:
# 临近亚洲国家和地区
# Malaysia
# 马来群岛
deny from 60.48.0.0/14 60.52.0.0/15 60.54.0.0/16 124.217.224.0/19 202.58.80.0/20 202.71.96.0/20 202.75.32.0/19 203.223.128.0/19 210.187.49.0/25 218.111.0.0/16 218.208.12.64/27
# Philippines
# 菲律宾
deny from 85.92.152.0/21 222.127.32.0/19 222.127.64.0/19
# Singapore
# 新加坡
deny from 59.189.0.0/16 116.14.0.0/15 165.21.0.0/16 219.74.0.0/16 219.75.0.0/17
# Thailand
# 泰国
deny from 58.9.0.0/16 58.137.13.0/24 61.19.64.0/18 117.47.0.0/16 202.28.0.0/15 202.44.135.0/24 203.107.142.0/24 203.113.13.0/24 202.143.128.0/18 203.144.128.0/17 203.148.128.0/17 203.149.0.0/18 203.150.128.0/17 203.151.38.0/24 203.155.0.0/16 203.172.128.0/17 222.123.0.0/16
# Vietnam
# 越南
deny from 58.187.112.0/20 125.234.0.0/15 203.113.128.0/18 203.162.0.0/16
# End Chinese/Korean blocklist
# 中国/韩国 禁止列表 结束
# Add other blocked domain names or IP addresses here, starting with “deny from ” without quotes
# 这里添加其他你需要禁用的ip地址或者郁闷,用 “deny from” (不包含引号)
# If you find that you need to poke a hole in the blocklist, for legitimate visitors, follow this example: allow from 123.456.789.0
# 如果需要添加例外的话,使用 allow from, 例如:allow from 123.456.789.0
# Add “allow from” IP addresses, or CIDR Ranges, after all of the “deny from” items, just before the closing Files tag.
# 在 Files 标签结束前,全部 “deny from” 后,添加 “allow from” 来增加特例
# Everything not included within these deny from ranges is PERMITTED by the allow portion of the directive.
# 本文中没有添加的ip或者ip段即为可以访问ip
# This prevents web browsers or spiders from seeing your .htaccess directives:
# 下面这个用以保护你的.htaccess不被蜘蛛或者网页浏览器获取。
deny from all
# End of file
.htaccess翻译自:http://www.wizcrafts.net/chinese-blocklist.html,记得翻墙(推荐一个在线代理)。

简便一点的生成 .htaccess 的网站有 http://www.blockacountry.com/

注:只要把生成出来的文档下载下来,上传到你网站的根目录并且改名为 .htaccess 即可,如果你根目录下原本就有这个文件,请不要覆盖,下载原有的 .htaccess 把代码添加进去即可。

比如你限制了中国 IP ,但是你自己也在中国,那怎么办?有个办法就是在 .htaccess 里允许你的IP访问,具体的方法如下:

order deny,allow <– 注意这里的写法
allow from 174.7.174.77 <– 这里输入自己的IP或IP段
allow from 174.7.0.0/16 <– 这里输入自己的IP或IP段
deny from 58.14.0.0/15
deny from 58.16.0.0/16
…..
介绍下 order 的写法:

order allow , deny 只要deny的IP就不能访问,不管是否allow了
order deny, allow 只要allow的IP就可以访问,不管是否deny了

介绍下 IP 段的写法:
allow from 174.7.0.0/16 的意思就是允许 174.7.0.0 – 174.7.255.255 IP 段的访问,而这个 174.7.0.0/16 IP段是 CIDR 的形式。

注:CIDR 形式该怎么理解呢?
其实很简单,把我们常见的那种 IP 转换成二进制的32位形式,那么 CIDR 写法中”/16 就是把其中的前16位固定不变,而后面的16位数,每八位一组,可以改变。所以就有了上面的范围。
再举个例子, 192.168.88.12/31 ,后面只有一位可以改变,那么这个 IP 范围就是:192.168.88.12 – 192.168.88.13 。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部