Where to add geolocation code to block one country access?

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

to .htaccess or where?

Does it works on shared hosting?

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]

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

I hope this helps.

1 Like

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.

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

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.

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

Only this code works:

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