SSL hostname in Heroku with Rails 3.1

I currently have a setting in which 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

I would like to use https://secure.example.comwhen using SSL, but I continue to use http://example.comwhen I do not use SSL. Is there a way to switch between host names depending on if I use SSL?

+5
source share
1 answer

, SSL Rails, , HTTP HTTPS , , , .

>= 3.1

config.force_ssl = true .

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.force_ssl = true
  end
end

https Rails. , HTTPS /.

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.force_ssl = false
  end
end

# config/environments/production.rb
MyApp::Application.configure do
  config.force_ssl = true
end

Rails < 3.1

, Rails 3.1, . HTTPS, .

config.middleware.insert_before ActionDispatch::Static, "Rack::SSL"

, Im Rack::SSL , Rails. , , ActionDispatch::Static ActionDispatch::Cookies.

Rack:: SSL Gemfile.

# Gemfile
gem 'rack-ssl', :require => 'rack/ssl'

HTTPS HTTP

Rack::SSL . :exclude, , / HTTPS.

Rack::SSL , HTTPS-.

config.middleware.insert_before ActionDispatch::Static, Rack::SSL, :exclude => proc { |env| env['HTTPS'] != 'on' }

URL- , Rack::SSL.

https://secure.example.com
http://example.com

+4

All Articles