I want to know how to block specific countries from certain pages of my website. I want to block 4 countries or so from just my main page and another page. Can this be done cleanly? If so how?
Note: I would like the other pages that I don’t include to work
Yes. Obtain an ip to location database (free ones are available, with lower accuracy than the paid versions), and use that to identify where the visitor is. You can either put the access logic in the pages (through some server side code which uses the database), or at the web server level (through MaxMind’s mod_geoip for Apache).
That is about as easy as it gets to be able to do that. Of course you also need to keep the list up to date as IPs are occasionally moved between countries. Also there is nothing to prevent someone in a banned country using a proxy server in a country that isn’t banned to be able to bypass the block.
I use htaccess to block out undesirable countries from accessing my website. Before you do something like this though I would consider the undesired impact that this could have for your legit visitors.
Even if your target demographic is local, you might be blocking out some legit visitors who are using AOL as their ISP. AOL uses proxies and someone visiting your website might live in your town, but the IP address they are connecting from could be coming from Europe… or anywhere. Still, there is good reason, in my opinion, to block out certain countries altogether. I know many on here would disagree, but some countries just have too much “bad” traffic to be trusted anymore. Use Google to find out more, I am unable to post links yet!
If you are using WHM/cPanel then it is easy to block entire contry IP ranges from firewall like CSF. For more details how to block entire country in CSF visit following URL:
I have found that blocking countries is a very effective way to reduce spam as well. It is also a good idea to block the proxies that they use as well.
There are several ways to implement country blocking but it is always a good idea to use a solution that prevents problematic users from coming back.