Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Show visitors from EU countries a page with GDPR message using Cloudflare and .htaccess

Turn on Cloudflare IP Geolocation

In your .htaccess file:

RewriteEngine on

# Set the CF-IPCountry header value as "Country" variable
SetEnvIf CF-IPCountry "(.*)$" Country=$1

RewriteCond %{ENV:Country} AT
RewriteCond %{ENV:Country} BE
RewriteCond %{ENV:Country} BG
RewriteCond %{ENV:Country} CY
RewriteCond %{ENV:Country} CZ
RewriteCond %{ENV:Country} DK
RewriteCond %{ENV:Country} EE
RewriteCond %{ENV:Country} FI
RewriteCond %{ENV:Country} FR
RewriteCond %{ENV:Country} DE
RewriteCond %{ENV:Country} GR
RewriteCond %{ENV:Country} HU
RewriteCond %{ENV:Country} HR
RewriteCond %{ENV:Country} IE
RewriteCond %{ENV:Country} IT
RewriteCond %{ENV:Country} LV
RewriteCond %{ENV:Country} LT
RewriteCond %{ENV:Country} LU
RewriteCond %{ENV:Country} MT
RewriteCond %{ENV:Country} NL
RewriteCond %{ENV:Country} PL
RewriteCond %{ENV:Country} PT
RewriteCond %{ENV:Country} RO
RewriteCond %{ENV:Country} SK
RewriteCond %{ENV:Country} SI
RewriteCond %{ENV:Country} ES
RewriteCond %{ENV:Country} SE
RewriteCond %{ENV:Country} GB

RewriteRule ^(.*)$ http://yourwebsite/blockedGDPR/ [R,L]

Create a file named index.html in blockedGDPR directory with the below content:

   GDPR Block
  We are unable to bear GDPR compliance cost at the moment.
    body {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      color: #2c3e50;
      margin-top: 20px;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.