大网数据广告活动
网站运营与故障问答

Block IP range using htaccess on Apache

· 52CE 我爱测

I'm looking for a good way to block an entire IP range using .htaccess . The problem I'm having is that the block I need to ban is 100 IP ranges big. From a programmer's perspective it should be easily doable, but I'm failing to …

问题原文

原帖链接 · LPChip · CC BY-SA 4.0

I'm looking for a good way to block an entire IP range using .htaccess.

The problem I'm having is that the block I need to ban is 100 IP ranges big. From a programmer's perspective it should be easily doable, but I'm failing to get it done without adding 100 lines in my .htaccess file, and other techniques I tried did not work.

The IP Address range I need to block is for HUAWEI Clouds:

  • 159.138.100.* to 159.138.199.*

A keen eye spots that basically 159.138.1 is enough to block.

I tried the following without success:

Order Allow,Deny
Deny from 159.138.1
Allow from all

I also tried:

Order Allow,Deny
Deny from 159.138.1*
Allow from all

How can I block this IP range effectively using .htaccess?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

IP ranges are specified in .htaccess using CIDR notation. The simplest rule that you could use would be

Deny from 159.138.0.0/16

Which would block slightly too much:

CIDR Range      159.138.0.0/16
Netmask         255.255.0.0
Wildcard Bits   0.0.255.255
First IP        159.138.0.0
Last IP         159.138.255.255
Total Host      65536

To block the exact range of 159.138.100.0 to 159.138.199.255 you would need several rules:

Deny from 159.138.100.0/22
Deny from 159.138.104.0/21
Deny from 159.138.112.0/20
Deny from 159.138.128.0/18
Deny from 159.138.192.0/21

Source: IP Range to CIDR calculator

I'm not sure where you got the IP range you are working with. This site lists 74 CIDR ranges for Huawai cloud service

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。