强制 Rails 上的 Ruby 应用加载 HTTPS 版本

Force Ruby on Rails app to load on HTTPS version

我在 Rails 运行 上有我的 Ruby 的 HTTP 和 HTTPS 版本,如果有人通过 HTTP 访问,我想自动重新加载 HTTPS 版本。

我以前在 Apache 上用 .httaccess 做过这个。如何在 Rails 上完成?

谢谢

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

config.force_ssl = true

将下面的代码放在config/applcation.rb

中即可实现
#config/application.rb
config.force_ssl = true

查看此 blog and this SO post 了解更多信息。

您也可以通过以下方式实现:

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

  private

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