Block Certain Users from Accessing your website

Many of us have websites dedicated to certain markets or countries. However, due to the Internet being a global space, we’ll often end up delivering traffic to users that may not be within our target audience.

At other times, we get annoyed by certain users on our websites that usually wreck havoc by either spamming the forum/blog we’re running or cause other annoyances to the site visitors.

There is a handy tool that we can use to filter out the visitors we don’t want while maintaing site accessibility to the others. It is called “.htaccess” file. The .htaccess file if placed inside your web root folder (typically public_html), it can simplify the way you manager your website.

For instance, to block a certain IP address from accessing our website, we would create a file called: .htaccess and save the following content inside it:

<Limit GET HEAD POST>
order allow,deny
deny from 41.204.224.50
allow from all
</limit>

The above code would block the user on IP address: 41.204.224.50 from accessing your web page. Obviously, you’ll have to remember that most users on the internet are assigned temporarily IP addresses that can change from time to time. So block a single IP address will likely not permanently block that user.

Nevertheless, this idea can be further expanded to block certain regions or countries in the world from accessing your website. Lets say you’re having some trouble with users visting your website from Nigeria. You wish to block access to your website from anywhere in Nigeria. First, you’ll need to obtain list of IP address blocks used in Nigeria. A very handy site for that is http://blockacountry.com which offers as comprehensive of a list as possible which associates IP addresses to countries. NOTE: This list may be incomplete or may contain errors. You should be very careful when blocking a whole country as that may include innocent users that reside outside of that country who may be leasing IP addresses in that same network block.

Follow the steps on the blockacountry.com page which will generate your .htaccess code. Copy/Paste that code into your .htaccess file, and upload it to your Unix based server. This will deny access to any site visitor coming from the black-listed IP addresses.

For our Shared Unix users on cPanel servers, there is a simple way to add an IP address to your block list. This is typically available under your cPanel control panel interface -> Security section -> IP Deny Manager icon.