IP2Location Country Blocking

If using WordPress without a caching plugin, you may wish to review this plugin first – IP2 Location Country Blocker

https://www.ip2location.com/free/plugins/blog

I prefer use of iptables to this method. If you wish to use IP2 Location for free, to block countries, this article shows the configurations for EC2 httpd (apache) or (I assume) Debian apache2.

We download the IP2 Location lite database and programs.Once configured, we add entries to the .htaccess file fr which countries to block. We cannot add which countries Not to block.

Create an account with ip2location, and access the free country blocking content:

https://lite.ip2location.com/
sign in… (or create an account)

Click on Download, and under IP2Location Database, download the DB1.LITE IP_Country IPv4 database BIN file.

Then go to:

https://www.ip2location.com/development-libraries/ip2location/apache

Download the Apache module.

The go to:

https://www.ip2location.com/documentation/ip2location-libraries/c

Download the C library.

 

We do not go into use of NGINX as this requires compilation and I think iptables is more effective anyway. If yo want to block huge numbers of countries then you would probably consider IP2location and work out if you can block everything except your own country etc. I never got that working.

In the Installation steps, step number 2, you will see “Download IP2Location C library from here”, so click on the link to download the C Library as well.

You now have three files:

IP2LOCATION-LITE-DB1.BIN.ZIP
ip2location-apache-master.zip
IP2Location-C-Library-master.zip

I have attached a zip file from 1 July 2025 below if you have problems or want to test the above files.

Create a directory, e.g. /home/ec2-user/ip2location, upload the files to it and unzip them as shown below:

(You can download a version of the files below from July 2022 if it helps for testing or to see what the files are…)

cd /home/ec2-user
cd /home/ec2-user/
mkdir ip2location
chmod 2775 ip2location
cd ip2location

[These are the files you will have: ip2location-apache-master.zip  IP2Location-C-Library-master.zip  IP2LOCATION-LITE-DB1.BIN.ZIP]

unzip ip2location-apache-master.zip
unzip IP2Location-C-Library-master.zip
unzip IP2LOCATION-LITE-DB1.BIN.ZIP

[Check you have installed the following packages:]

dnf -y install autoconf automake libtool httpd-devel

cd IP2Location-C-Library-master
vi Makefile.am
[After the line AM_CPPFLAGS add the following:]
ACLOCAL_AMFLAGS = -I m4

[Save and exit the editor]

vi configure.ac
[After the line AC_C_BIGENDIAN add the following:]
AC_CONFIG_MACRO_DIR([m4])
[After the line AC_PROG_LIBTOOL add the following:]
AM_PROG_CC_C_O

[Save and exit the editor]

[Run these commands:]

mkdir m4
autoreconf -i -v --force
./configure
make
make install

[Run these commands:]

cd ..
cd ip2location-apache-master
apxs -i -a -L /usr/local/lib/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c
ln -s /usr/local/lib/libIP2Location.so.1 /usr/lib/libIP2Location.so.1

[Then add these lines to the end of your /etc/httpd/conf/httpd.conf file:]

<IfModule mod_ip2location.c>
    IP2LocationEnable On
    IP2LocationDetectProxy On
    IP2LocationSetmode ALL
    IP2LocationDBFile /home/ec2-user/ip2location/IP2LOCATION-LITE-DB1.BIN
</IfModule>

[You will notice this new line: LoadModule IP2Location_module /usr/lib64/httpd/modules/mod_ip2location.so is now in httpd.conf]

[Fix file permissions:]

cd /home/ec2-user/ip2location
find . -type d -exec sudo chmod 2775 {} \;
find . -type f -exec sudo chmod 0664 {} \;
chmod 777 /home/ec2-user/ip2location/IP2LOCATION-LITE-DB1.BIN
cd /home/ec2-user
chown apache ip2location
chgrp ec2-user ip2location

[Restart httpd:]

systemctl restart httpd


Use the IBM 2 letter country codes to select your countries, and add lines like these near the top of your .htaccess file:
https://www.ibm.com/docs/en/iis/9.1?topic=sets-iso-territory-codes
I place the lines below my https redirect lines: (use your own domain name - note, some people do not use redirection as I have shown)
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain.au/$1 [R,L]


vi /var/www/html/.htaccess

RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^RU$
RewriteRule ^(.*)$ https://google.com.au [L]
RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^CN$
RewriteRule ^(.*)$ https://google.com.au [L]

[save and exit]

You can test after adding entries into .htaccess

You may wish to explore what the IP2Location sites have to offer.

For example, with WordPress:

https://www.ip2location.com/free/plugins/blog

Start typing and press Enter to search