How do I block an IP address from viewing my site?

You can add lines to your .htaccess file to block an IP address from viewing your site. For instance, the following line will block all requests from IP address

Deny from

You can block larger IP address blocks as well. For example, to block every IP address beginning with 192.0.2, you could use either of these:

Deny from
Deny from 192.0.2

The Apache Web server allow and deny documentation explains the options in more detail.


Unfortunately, there are a couple of limitations to this approach.

First of all, some people have Internet connections where they’re assigned a different IP address every time they restart their modem or computer, so their IP address can change each day. Secondly, more determined visitors can use a free proxy service to completely hide their IP address.

In short, trying to block access based on an IP address is unlikely to stop anyone who really wants to see your site. In many cases, this means it's not worth the continual effort to try to block an ever-changing set of IP addresses.