We help IT Professionals succeed at work.

htaccess block all countries but the US

Last Modified: 2017-04-19
Is there a good and programmatic way, possibly with my htaccess or a wordpress plugin to only allow US based traffic see my site, blocking all countries but the US?
Watch Question

Senior Platform Engineer
Unlock this solution and get a sample of our free trial.
(No credit card required)
Most Valuable Expert 2011
Author of the Year 2014

I agree with Phil about MaxMind.

I wrote an article on this years ago.  It wasn't about blocking, but it shows some of the PHP way of approaching the issue.  Probably can generalize this into something for .htaccess if you would prefer that approach.
Dr. KlahnPrincipal Software Engineer

I find geolocation to be correct about 3/4 of the time ... which is somewhat better than flipping a coin.  With the shortage of IPv4 blocks becoming more pressing, IP blocks supposedly assigned to the US are being used by non-US companies, and vice versa.  Further, when a connection comes in on Tor or over a VPN, there's no telling where the actual origin of the connection is located.

Stronger blocking is more effective but there's a cost associated with a lost sale or lost exposure to a potential customer.  At some point you have to throw up your hands and say "From this point forward, we just have to tolerate it as best we can."

If you're seeing trouble emanating from non-US IP blocks, it may be of interest to mention that using iptables to stop connections from all ex-USSR and APNIC blocks (except AU and NZ) cut a number of "issues" down substantially on our server.
Most Valuable Expert 2011
Author of the Year 2014


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.