bestshop24h
bestshop24h

Reputation: 121

about blocking a country by using .htaccess

I have tried many times, I feel desperate. I have been able to do this successfully before on Apache 2.2. Now my environment is Apache/2.4.18 (Ubuntu) Server

I go to https://www.ip2location.com/blockvisitorsbycountry.aspx to generate code

  1. select ipv4
  2. select China
  3. select "Apache 2.4 .htaccess deny"

Then add the code to the bottom of .htaccess.

However, the result is that I cannot access the website whether I use a VPN or not.

I am located in China.

Later, I tried to select another one country, such as Japan, to generate new blocking code.

However, the result was the same, after adding the code into .htaccess. I also cannot access the website, even though the blocked country is Japan and I am in China.

I doubt the code, what should I do in order to get the right blocking a country code?


Some of the generated code:

<RequireAll>
Require all granted
Require not ip 1.0.1.0/24
Require not ip 1.0.2.0/23
Require not ip 1.0.8.0/21
Require not ip 1.0.32.0/19
Require not ip 1.1.0.0/24
Require not ip 1.1.2.0/23
Require not ip 1.1.4.0/22
:

Upvotes: 2

Views: 9133

Answers (2)

Rizwan Patel
Rizwan Patel

Reputation: 5

Another solution using GeoIP and .htaccess

I have hostinger server To enable GeoIP, all you have to do is add the following line to your .htaccess file:

GeoIPEnable On

Once added, you can verify if it's working by creating a PHP Info file and checking the PHP Variables section: Image showing GeoIP is enabled in your server

How to use it? To block access for perticular file in Britain, US and India:

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(GB|US|IN)$
RewriteRule (wp-login|xmlrpc).php$ - [F,L]

To allow access for whole website in Canada, UAE and India:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|AE|IN)$ RewriteRule ^(.*)$ - [F,L,QSA]

Upvotes: 0

bestshop24h
bestshop24h

Reputation: 121

now,I have solved the problem.

first of all, the code generated from https://www.ip2location.com/blockvisitorsbycountry.aspx is 100% correct!

the reason why I cannot access the website whether using VPN or not,is that I didnot add the following code to yourdomain.conf to make https(ssl) url also support rewrite.

    <VirtualHost *:443>
    .......

   <Directory "/var/www/html/yourdomainname/public_html">
       AllowOverride All
       Require all granted
   </Directory>
    </VirtualHost>

Upvotes: 1

Related Questions