Skip to content

Instantly share code, notes, and snippets.

@aliang
Created June 6, 2011 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliang/1010487 to your computer and use it in GitHub Desktop.
Save aliang/1010487 to your computer and use it in GitHub Desktop.
Force SSL on routes, version 2, doesn't use Devise
YourApplication::Application.routes.draw do
class SSL
def self.matches?(request)
# This way you don't need SSL for your development server
return true unless Rails.env.production?
request.ssl?
end
end
# Reusable Proc for redirection. Will redirect with 301 (on Rails 3.0, at least)
ssl_for_request = Proc.new { |_, request|
"https://" + request.host_with_port + request.fullpath }
constraints SSL do
# Routes go here
match "/give_me_your_credit_card_information", :to => "pay_me#credit_card"
end
# Redirect to SSL from non-SSL so you don't get 404s
match "/give_me_your_credit_card_information", :to => redirect(ssl_for_request)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment