Created
July 25, 2013 22:31
-
-
Save louy/6084371 to your computer and use it in GitHub Desktop.
Apache .htaccess geographical redirect based on CloudFlare's geo-ip headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
Nice! Just saved me a bunch of time. Thanks!
Thanks a lot, very helpful!
Sorry for commenting on an older gist, but how would you pass a no-redirect? So if we wanted to escape a redirect, would it be http://yoursite.com/?noredirect I need to be able to escape out of the redirect when a user clicks are certain link.
Thanks!
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 ?
Best version I've seen, ever.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Country codes: http://dev.maxmind.com/geoip/legacy/codes/iso3166/