I currently have a setup where I force SSL or http where I need it with this before_filter in my application controller:
def force_ssl
if params[:controller] == "sessions"
if !request.ssl? && Rails.env.production?
redirect_to :protocol => 'https://', :status => :moved_permanently
end
else
if request.ssl? && Rails.env.production?
redirect_to :protocol => 'http://', :status => :moved_permanently
end
end
end
What I’d like to do is to use https://secure.example.com when using SSL but keep using http://example.com when not using SSL. Is there a way I can switch between the hostnames depending on whether I’m using SSL?
First I’ll show how to force SSL in current and earlier versions of Rails, then at the end I’ve posted how to use HTTP and HTTPS in Parallel with each other, which is what I think your looking for.
Rails >= 3.1
Simply use
config.force_ssl = truein your environment configuration.You can also selectively enable https depending on the current Rails environment. For example, you might want to keep HTTPS turned off on development, and enable it on staging/production.
Rails < 3.1
Just in case you have any projects that are not Rails 3.1 and want the same feature. Enable HTTPS by adding the following line to your environment configuration.
config.middleware.insert_before ActionDispatch::Static, "Rack::SSL"Note that I’m passing
Rack::SSLas string to delegate the loading of the class at the end of the Rails application initialization. Also note the middleware must be inserted in a specific position in the stack, at least beforeActionDispatch::StaticandActionDispatch::Cookies.Don’t forget to define Rack::SSL dependency in your Gemfile.
Enabling HTTPS and HTTP in parallel
Rack::SSLhas a very interesting and undocumented feature. You can pass an:excludeoption to determine when to enable/disable the use of HTTPS.The following code enables
Rack::SSLand all its filters only in case the request comes from a HTTPS connection.Both the following URLs will continue to work, but the first one will trigger the
Rack::SSLfilters.https://secure.example.comhttp://example.com