简体   繁体   中英

Force Ruby on Rails app to load on HTTPS version

I have an HTTP and HTTPS version of my Ruby on Rails running, and if someone accesses through the HTTP one, I want to automatically reload the HTTPS version.

I have done this before on Apache with .httaccess. How can I do it on Rails?

Thanks

在您的production.rb文件中(或您要强制ssl的任何环境中)添加

config.force_ssl = true

You can achieve it by putting the below code in config/applcation.rb

#config/application.rb
config.force_ssl = true

Check this blog and this SO post for more info.

You can also achieve that by:

class ApplicationController < ActionController::Base
  force_ssl if: :ssl_enabled?

  private

  def ssl_enabled?
    %w(staging production).include?(Rails.env)
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM