Skip to content

Instantly share code, notes, and snippets.

@louy
Created July 25, 2013 22:31
Show Gist options
  • Save louy/6084371 to your computer and use it in GitHub Desktop.
Save louy/6084371 to your computer and use it in GitHub Desktop.
Apache .htaccess geographical redirect based on CloudFlare's geo-ip headers
# add as many as you need...
SetEnvIf CF-IPCountry SY RedirectSubdomain=syria
SetEnvIf CF-IPCountry AE RedirectSubdomain=uae
SetEnvIf CF-IPCountry EG RedirectSubdomain=egypt
# Only redirect if Host is not a subdomain
SetEnvIfNoCase Host ^.+\.example\.com$ !RedirectSubdomain
# Only redirect if cookie "noredirect" doesn't exist
SetEnvIfNoCase ^Cookie$ noredirect=true !RedirectSubdomain
RewriteEngine On
# Set cookie "noredirect"
RewriteCond %{ENV:RedirectSubdomain} .+
RewriteRule .* - [co=noredirect:true:.example.com:7200:/] #7200 minutes
# Redirect to subdomain
RewriteCond %{ENV:RedirectSubdomain} .+
RewriteRule ^ http://%{ENV:RedirectSubdomain}.example.com%{REQUEST_URI} [L,R]
@franmastromarino
Copy link

hello mate, im trying to do something similar. Instead of subdomains i want to redirect users to the same url with the /?lang=XX parameter

do you know how can i do it ?

@Cam
Copy link

Cam commented Sep 8, 2018

Best version I've seen, ever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment