Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created April 9, 2015 23:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahemoff/8ec27ebfe22b89c8669a to your computer and use it in GitHub Desktop.
Save mahemoff/8ec27ebfe22b89c8669a to your computer and use it in GitHub Desktop.
Cloudflare country detection (geo-ip) in Ruby/Rails
class ApplicationController < ActionController::Base
def cloudflare_country_code
request.headers['HTTP_CF_IPCOUNTRY']
end
def eu_request?
cloudflare_country_code.in? %w(GB FR DE ..... )
end
end
@mackinven
Copy link

mackinven commented Nov 30, 2017

This worked great for me.
To serve a different terms and conditions page to the EU I also added this to the appropriate controller:

  def terms
   	if eu_request?
      render :template => "site/terms_eu"
    else 
      render :template => "site/terms"
    end
  end

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