IP2Location Country Blocking

IP2Location Country Blocking

IP2Location Country Blocking

IP2 Location has an individual user login at no cost, where you can download the “lite” country database for those IP addresses.
the .htaccess file can alternatively be appended with the IP addresses of a few countries if you wish.

IP2location Country Blocking – Linux 2023

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_Countrt IPv4 database BIN file.

Then go to:

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

Download the Apache module.

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

Create a directory, 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
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)
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 – will let you think about how to do that :-)