Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:52:23+00:00 2026-05-27T12:52:23+00:00

I currently have a setup where I force SSL or http where I need

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:52:23+00:00Added an answer on May 27, 2026 at 12:52 pm

    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 = true in your environment configuration.

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

    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.

    # 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

    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::SSL as 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 before ActionDispatch::Static and ActionDispatch::Cookies.

    Don’t forget to define Rack::SSL dependency in your Gemfile.

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

    Enabling HTTPS and HTTP in parallel

    Rack::SSL has a very interesting and undocumented feature. You can pass an :exclude option to determine when to enable/disable the use of HTTPS.

    The following code enables Rack::SSL and all its filters only in case the request comes from a HTTPS connection.

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

    Both the following URLs will continue to work, but the first one will trigger the Rack::SSL filters.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a unittest.TestCase that looks like.. class test_appletrailer(unittest.TestCase): def setup(self): self.all_trailers =
I currently have mirroring setup between three computers, principle, mirror, and witness. During the
I currently have replication setup on MySQL 4.1 (master, named radius) to MySQL 5.0
I currently have a news website setup in PHP/MYSQL that's a bit old and
Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database.
currently I'm trying to have a setup where a generic database is distributed to
Currently, I have a project with a Windows Service. I also created another Setup
Currently I have two sites setup in IIS. Primary Site (Uses HTTPs and requires
I currently have a NetBeans php project setup to sync from my user directory
I have setup jgrowl and everything seems to working correctly. I currently have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.