如何强制使用SSL并使用www域网址重定向?

时间:2018-06-12 06:14:44

标签: ruby-on-rails google-chrome heroku https hsts

我在example.com上运行了一个域,在Heroku上运行了https://example.com。我的名称服务器上有一个URL重定向,从www到https://example.com。我在config.force_ssl = true中设置了config/environment/production.rb。域URL重定向仅适用于http,而不适用于https。它在我第一次输入www.example.com时可以在Chrome中使用。但是第二次使用www.example.com失败了。它给出了一个' ERR_CONNECTION_REFUSED'使用307内部重定向。

Status Code: 307 Internal Redirect
Location: https://www.example.com/
Non-Authoritative-Reason: HSTS

尽管域名服务始终通过Location: https://example.com提供curl。 Chrome从哪里获得https://www*?我看到了https://superuser.com/a/881431/130929关于在chrome://net-internals/#hsts删除Chrome中的HSTS条目的问题。如果我同时为example.com和www.example.com执行此操作,那么它仅适用于www.example.com的第一次。 example.com和https://example.com始终有效。更糟糕的是,在Firefox中,加载页面后,简单地使用example.com失败,因为Firefox会自动添加https://www。如何在根域中使用HTTPS并将www重定向到根域?我并不关心处理https://www.example.com,因为没有人会这样做。他们只会错误地输入www.example.com。

1 个答案:

答案 0 :(得分:0)

好的我用这个答案修改了http://stackoverflow.com/questions/10629397/ddg#10632901

我在curl -i https://example.com看到它正在返回

Strict-Transport-Security: max-age=15552000; includeSubDomains

最大年龄为6个月。 includeSubDomains可能意味着包含www。所以我添加了

class ApplicationController < ActionController::Base
  before_action :disable_hsts_subdomains

  def disable_hsts_subdomains
    response.headers["Strict-Transport-Security"] = 'max-age=15552000;'
  end

刚刚删除了includeSubDomains,因此它不会尝试重定向www而不首先联系到主机(DNS服务器),这会将重定向发送到正确的根域。

相关问题