Blocking IP Addresses With .htaccess

What is .htaccess

.htaccess is a file that Apache reads that allows configuration changes on a per-directory basis. One of the handy little tricks one can do with this configuration file is block visitors from viewing a web site.

The Basics – Blocking 1 IP Address

Order allow,deny
Deny from 192.168.0.101
Allow from all

This will refuse all access (GET & POST) to the web site for the IP address 192.168.0.101

The Multiple IP Approach

Order allow,deny
Deny from 192.168.0.101
Deny from 192.168.0.102
Deny from 192.168.0.103
Deny from 192.168.0.104
Allow from all

Blocking IP Ranges

Order allow,deny
Deny from 192.168.0 ßnotice there is no 101, 102, 103 (this affects a lot of users)
Allow from all

IP’s range from 0 – 255 (ex -192.168.0.0 – 192.168.0.255)
You may also block ranges by way of calculating CIDR (Classless Inter-Domain Routing – pronounced “cider”) IP ranges (8, 16, 24bits)

Order allow,deny
Deny from 192.168.0/16
Allow from all

Get Mean

You can also block host names instead of numeric addresses.

Order allow,deny
Deny from cox.net
Allow from all

This will block anyone in the Cox.net network. But beware; banning large isp companies such as Cox.net or Comcast.com will block all their users. Do your research when it comes to blocking hostnames. Think about who may be affected.

There are many other ways of blocking IPs, but using .htaccess is one of the more common requests we receive.

Share
1,788 views
Bookmark the permalink.

Comments are closed.