Block a country using mod_geoip

Do you have a list of countries that you want to block access to your site?  Have you tried adding IP numbers into your .htaccess file only to generate a HUGE .htaccess file and a never-ending stream of IP numbers?  How about using mod_geoip!!

mod_geoip is a module that can be compiled into Apache on the build.  Generally shared hosting providers will not have this module loaded, however, if you are on a VPS or a Dedicated Server you can compile this module into Apache.

Then just do the following

Edit your .htaccess file

# Add more countries here
Deny from env=BlockThese

A full list of 2 digit country codes can be found here:

The above listed in the example is "CN = China" and "TR = Turkey"

You could also just reverse the .htaccess to make it ALLOW and enter US or UK as the country code.

If you don't have mod_geoip installed, you could use the output generated through
TSS makes no warranty or representation to how your site will perform if you include blocks on your site.  Keep in mind that the larger the .htaccess file, the slower your site loadings will be as it has to process the .htaccess file on each loading.  IP numbers can also change and/or be added to certain country ranges.  You will need to remember to update this list accordingly.

Reviewed 10/18/2016

  • Email, SSL
  • 168 Users Found This Useful
Was this answer helpful?

Related Articles

How to add Expires Headers for caching files

We've had many users asking about compressing their website using mod_gzip or mod_deflate and...

301 Redirect from non-www to www -- Without Looping!

So - you want to improve your search engine rankings, and SEO gurus told you that Google might be...

Force folder to redirect to https mode

Sometimes webmasters need to force particular folders to load in HTTPS mode.  This is ideal for...

301 Redirect for All Domains

So you've purchased a ton of domains to park on your main account, but you don't want Google to...

Redirect all visitors except from IP number specified

From time to time it's necessary to block access to people from various parts of the website....