Skip to content

Instantly share code, notes, and snippets.

@rgarver
Created February 4, 2011 17:30
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 rgarver/811419 to your computer and use it in GitHub Desktop.
Save rgarver/811419 to your computer and use it in GitHub Desktop.
if you constrain on protocol and want to redirect to fix if someone hits http instead of https, use this.
Rails::Application.routes.draw do
...
SecureTheRoute = lambda do |env|
request = ActionDispatch::Request.new(env)
dest = "http#{request.ssl? ? '' : 's'}://" + request.host_with_port + request.fullpath
begin
Profounder::Application.routes.recognize_path(dest)
[301, {'Location' => dest}, ["Redirecting to #{dest}"]]
rescue ActionController::RoutingError
[404, {"Content-Type" => 'text/html'}, [File.read(Rails.root.join('public', '404.html'))]]
end
end
match '(*path)' => SecureTheRoute
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment