Banning Countries with .htaccess


I’ve got a growing list of IPs and country codes banned in .htaccess file but I’m getting really sick of spammers from other countries accessing my site for whatever reason. Is it possible to ban all countries except for the ones I specify? My site is really only geared for US, UK, CA and a few others. Can I do something akin to deny from all but US, UK, CA, etc.? I understand I’ll be cutting off many countries but frankly, that doesn’t bother me in the least since most of my traffic is from US anyway. Adding the entire list of country codes doesn’t seem efficient. Is there any way to get around this?




I was intrigued by your question, and did a little google, which pointed me to an interesting item:

“banning by country doesn’t work well for several reasons: First the domain name for the requesting IP must be available in reverse dns in order to get the country code as remote_host. That’s not always true, and reverse DNS is slow. Second, you are banning an IP assigned to the ISP, not necessarily the user or a group of users. The user and ISP could be in different countries by virtue of the international nature of telecom. Even banning by IP numbers assigned to countries doesn’t work all that well. There is no central “map” to tell you what address blocks go with what country - and they are assigned piecemeal and willy-nilly. There are, however, some nice subscription services to make the info available to you - big buck$, though…”

Original link is :

This sounds a little discouraging, but I thought you might be interested in this opinion :slight_smile:



Thanks so much rlparker. I think I’d seen that post as well but I guess I’m just so sick of spammers I was hoping to find a way around this.

Anyway, thanks again.



Why not just think about some kind of Human authentication system, or require registering with a valid E-mail address before they can post. This way you could leave the doors of your website open, and prevent (or seriously deminish) spam.



Banning people by country is not really in keeping with the spirit of the World Wide Web, is it?

Actually, a large number of the spammers in the world are in the USA.

– Dan