【问题标题】:redirecting from http://example.com to https://example.mysite.com从 http://example.com 重定向到 https://example.mysite.com
【发布时间】:2012-02-20 02:59:39
【问题描述】:

这个问题已经被以各种方式提出,但我还没有找到正确的组合来回答我的特定问题。

配置

  • Rails 3.1(允许我在我的ApplicationController 中使用force_ssl)
  • 托管在 Heroku Cedar 上(所以我无法触及中间件)
  • 我的 SSL 证书已注册为 secure.example.com

我已经将force_ssl 添加到我的ApplicationController,如下所示:

# file: controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  force_ssl
end

问题

目前,如果用户导航到 http://example.com,force_ssl 会切换到 SSL,但由于它不是 secure.example.com,它会显示有关未经验证的安全证书的警告,因为它使用的是默认 Heroku 证书。

(我已经验证导航到 http://secure.example.com 会正确重定向到 https://secure.example.com 并使用正确的安全证书。这很好。)

问题

如何强制http://www.example.com/anything 和http://example.com/anything 重定向到http://secure.example.com/anything? (我假设 force_ssl 将处理从 http 到 https 的切换。)由于我无法触摸中间件(回想一下这是 Heroku 托管),我假设我可以执行以下操作:

# file: controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  force_ssl
  before_filter :force_secure_subdomain

private
  def force_secure_subdomain
    redirect_to(something...) unless request.ssl?
  end
end

...但我还没有充分理解redirect_to 和请求对象,不知道为something... 写什么。 (我想确保它处理查询参数等)

【问题讨论】:

    标签: redirect ssl https ruby-on-rails-3.1 heroku


    【解决方案1】:

    您可以通过执行以下操作重定向到不同的主机名:

    # file: controllers/application_controller.rb
    class ApplicationController < ActionController::Base
      force_ssl :host => "secure.example.com"
    end
    

    请参阅:rails force_ssl source 了解更多信息

    【讨论】:

    • 您可能还想提供一个 :protocol => 'https' 参数。我在相关问题上看到了这个建议。
    【解决方案2】:

    您应该看看rack-rewrite - 它本质上是 Apache 重写的,但采用 Ruby 形式,可在 Heroku 上使用。

    这将允许您创建各种机架级规则以及应该发生什么重定向等以及何时发生。

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2014-10-02
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多