How I can block specific countries from accessing my website

MartonB

Regular Member
Joined
Aug 20, 2019
Messages
285
Reaction score
70
Hi
I'm trying to block visitors from different countries is there any other free methods that actually work

I tried using the .htacces file as mentioned here
Code:
https://www.hostinger.com/tutorials/htaccess/how-to-allow-or-block-visitors-from-specific-countries-using-htaccess

But it didn't work as I can see on my analytics. any ideas. thanks
 
There is feature in wordfence or manually ip s can blacklisted as well
 
With PHP and maxmind's free geo ip database.
I'm working with Wordpress, I'm not pretty sure if i can use this. as i see maxmind does not have many locations on the free download, as I'm targeting Asian countries
 
you may be able to block them with php, that is what i use. do you know why the htaccess didn't work?
 
Wordfence paid version

I think it kinda worked but not 100% let's say 10-20%.
besides, I was still getting visits from the country I'm blocking, I tried to browse the website with a proxy from the same country I'm blocking, I was successful to get in. i think the Ip database is outdated.
 
If you are using Apache, you can use mod_geoip.

Example to configure the .htaccess settings

Code:
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE US BlockCountry
# ... place more countries here

Deny from env=BlockCountry

I don't think if i can do that, I'm using a shared hosting plan. but this still a good option. i have downloaded geoip-api-mod_geoip2-1.2.10.zip.
Any idea how i can install it on my Cpanel? i can do paths and stuff but i'm not seeing the /path/to/GeoIP.dat file on the ZIP file. or i'm having the wrong link?
 
I don't think if i can do that, I'm using a shared hosting plan. but this still a good option. i have downloaded geoip-api-mod_geoip2-1.2.10.zip.
Any idea how i can install it on my Cpanel? i can do paths and stuff but i'm not seeing the /path/to/GeoIP.dat file on the ZIP file. or i'm having the wrong link?
Minimal resources I suggest to integrate a Cloudflare account
 
If you are using Apache, you can use mod_geoip.

Example to configure the .htaccess settings

Code:
GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE US BlockCountry
# ... place more countries here

Deny from env=BlockCountry
this is used heavly works free and fine .
 
As of 2019, MaxMind country DB can be used as follows:
Code:
<?php
require_once 'vendor/autoload.php';
use MaxMind\Db\Reader;
$databaseFile = 'GeoIP2-Country.mmdb';
$reader = new Reader($databaseFile);
$cc = $reader->get($_SERVER['REMOTE_ADDR'])['country']['iso_code'] # US/GB...
$reader->close();
Source:https://github.com/maxmind/MaxMind-DB-Reader-php
 
Back
Top