Where to add geolocation code to block one country access?


#1

Where should I add this code:
Code:
if($country_code == ‘KW’){
exit; //or redirect or whatever
}

to .htaccess or where?

Does it works on shared hosting?


#2

Above code is written in another language which .htaccess doesn’t support. Please don’t add it in the .htaccess else your site will goes down.


Here’s the right way -

Suppose, you want to block all UK traffic. You can block via .htaccess, this method is free, limited to Cloudflare users.

# Block UK
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:CF-IPCountry} ^(GB)$
RewriteRule .* - [F]
</ifModule>

Note: Two letter country code is written in ISO 3166-1 Alpha 2 format.

I hope this helps.


#3

Hi
Tried your code. But it still enters the site (not easily) but still enters from that country. Its not a complete block. I mean some parts of the site appears some parts are not (on firefox) chrome and safari is okay.

I think cloudflare ignores the htaccess and serve itself not htaccess. Thats why your code isnt blocking that traffic.


#4

No it doesnt block. Cloudflare simply ignores the htaccess file.


#5

Make sure to keep GEO Location turned on at Network tab or page rule by URL. (Top priority)
Also, at DNS page records should be orange color. Means, proxy should be active.
Else, it will not work.


#6

Cloudflare respect .htaccess (it WILL bypass if there is cache everything rule).


#7

Only this code works:

SetEnvIf CF-IPCountry RU BuzzOff=1
Order allow,deny
Allow from all
Deny from env=BuzzOff