Skip to content

Instantly share code, notes, and snippets.

@zero-master
Last active February 14, 2022 20:07
Show Gist options
  • Save zero-master/57ce7f5091503ca637747df5ba4fe71d to your computer and use it in GitHub Desktop.
Save zero-master/57ce7f5091503ca637747df5ba4fe71d to your computer and use it in GitHub Desktop.
Show visitors from EU countries a page with GDPR message using Cloudflare and .htaccess

Turn on Cloudflare IP Geolocation

https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-Cloudflare-IP-Geolocation-do-

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:

<html>
  <title>
   GDPR Block
  </title>
  <body>
  We are unable to bear GDPR compliance cost at the moment.
  </body>
  <style>
    body {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      color: #2c3e50;
      margin-top: 20px;
    }
    </style>
    
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment