Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a website that needs to block a particular country based on IP address. I am more than aware that IP-based blocking is not a foolproof method for blocking visitors, but it is a necessary step in the right direction.

Since I'm using PHP, what I would do is use a GeoIP database like However, I'm curious to know if there's a better way of doing this.

The website is on a shared webserver (I don't have root access) and it is running Apache on centOS. I guess my question is "can an .htaccess file be configured to block by IP using an external source to lookup IP addresses."

share|improve this question
The accepted answer is no longer valid as of 8/9/12 with blockacountry being offline. – Brian Webster Aug 9 '12 at 23:15
up vote 4 down vote accepted

Use cautiously...I'm sure there are plenty of people on here that think you are doing the wrong thing by blocking whole countries.

share|improve this answer
Hmm, not always. Sometimes you're mandated by law to deny access to countries. Like Pandora, for example. – Mark Henderson Aug 3 '10 at 21:37
P.S. With the script that they generate, you should really get rid of the ErrorDocument 403 directive, or replace it with your own, becuase it redirects you to their own website and then launches popups. – Mark Henderson Aug 3 '10 at 21:38
I wrote this with this answer in mind:… – Jason Berg Aug 3 '10 at 21:40
Yes. This works quite well - and yes, I'm aware that blocking countries this way is not recommended, but it's a requirement and there's not much I can do about it. Good answer, by the way. – Nathan Osman Aug 4 '10 at 22:20
This is a great example of a non future-proof answer.… – Brian Webster Aug 9 '12 at 23:14

There seems to be a geoip module for apache, but the server admin might have reasons against installing it.

share|improve this answer
Ya, this would only be useful in a VPS or dedicated server setup. – Nathan Osman Oct 22 '10 at 0:13

If you find the additional rules causing CPU overhead and latency issues, TechGuard makes an in-line appliance for IP address blocking of inbound and outbound traffic, including commercial block lists of millions of IP addresses tied to malicious activity in countries allowed by your policy. It does all this without impacting network latency or TCP connections, while automatically updating what is equivalent to 12,000 IP ranges a day.

share|improve this answer
Welcome to Server Fault! Please read our faq in particular May I promote products or websites I am affiliated with here?. Two answers and two recs for the same website is close to the line. – Iain Sep 7 '12 at 21:05
I'm with @Iain: possibly useful answer, but phrasing sounds an awful lot more like marketing copy than the voice of bitter experience. – pjmorse Sep 23 '12 at 17:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.