kieranbarnes Independent PHP, WordPress & CubeCart Programmer

Block Countries in Apache

Posted on November 18, 2008

Assuming you have mod_geoip installed in apache we can block certain countries in 6 lines of code.

I had a client who was experiencing an inhumane amount of spam on their phpBB forums, which are notorious for bot attacks. Luckily this forum was geographically specific - so we can block all the countries we aren't interested in.

Here's the .htaccess code I put in the forums sub-directory


# START Block Countries

RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(GB|US)$
RewriteRule ^(.*)$ http://www.some-page-outside-the-forum-directory$1 [L]

# END Block Countries

This code will allow only Great Britain and the United States into the forum directory, any other country will be redirected to a page describing why they aren't allowed instead of simply blocking them.


Related posts

  1. Apache, JungleDisk and Port 80
    I was tearing what little hair I have trying to solve a very strange bug...
  2. mod_geoip Revisted including stopping spam in phpBB 2
    I've recently moved a client's forum onto a new cPanel server, previously it was on...
  3. NTLM Authentication (Active Directory) on Apache (Linux)
    Here is a quick guide to enabling NTLM authentication for Apache 2. I generally use...
  4. Ridiculously simple NTLM Authentication for Apache (Ubuntu)
    We all know Ubuntu makes things amazingly simple. This is the best I've found so...
  5. Remove Shopp Javascript from non-Shopp pages
    Following on from a post on the Shopp forum regarding this, a few people are...

Posted by Kieran