Deny visitors by IP address using .htaccess

The visitor blocking facilities offered by the Apache Web Server enable us to deny access to specific visitors, or allow access to specific visitors. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration area.To set-up visitors restrictions and blocking, create a .htaccess file following the main instructions and guidance which includes the following text:

order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

The above lines tell the Apache Web Server to block visitors from the IP address ‘255.0.0.0’ and ‘123.45.6.’, note the second IP address is missing the fourth set of digits, this means any IP address which matches the firth three set of digits will be blocked, e.g. ‘123.45.6.10’ and ‘123.45.6.255’ would be blocked.

To set-up blocking of all visitors except yourself, create a .htaccess file following the main instructions and guidance which includes the following text:

order allow,deny
allow from 255.0.0.0
deny from all

The above lines tell the Apache Web Server to block all visitors except those with the IP address ‘255.0.0.0’, which you should replace with your own IP address.

You may add any number of ‘deny from’ and ‘allow from’ records after the ‘order allow,deny’. Note the change from ‘allow from all’ to ‘deny from all’ on the bottom line, this is important and must be changed depending on your requirements. If you want to allow your visitor access, you would use ‘allow from all’ and place ‘deny from’ lines above.

Blocked visitors will be shown a ‘403 Forbidden’ error message. You can customise this error message by following the ‘Error Documents’ section of this article.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.