【问题标题】:rack-rewrite code to redirect https to http (on heroku)机架重写代码以将 https 重定向到 http(在 heroku 上)
【发布时间】:2012-06-11 16:35:45
【问题描述】:

不知何故,谷歌将我的主页索引为https://mydomain.com。当您进行 site:mydomain.com 搜索时,第一个结果是 https://mydomain.com,我没有 SSL 证书,也不想使用 https。现在,我们的访问者当然会在他们的浏览器中收到丑陋的警告(因为 heroku 默认提供他们的 *.heroku 证书)。

似乎我可以使用 rack-rewrite gem 进行 301 重定向,但我就是找不到方法。

那么,将所有 https:// 重定向到 http:// 的机架重写配方是什么?我能找到的只是关于如何做相反的事情或进行规范重定向的信息。

【问题讨论】:

  • 我正在尝试完成完全相同的事情。您最终使用了什么解决方案?

标签: ruby-on-rails heroku rack rack-rewrite


【解决方案1】:

嗯,未经测试,但是这样的东西可以吗?

r301 %r{.*}, 'http://non-secure-domain.com$&', :if => Proc.new {|rack_env|
  rack_env['SERVER_PORT'] != '80'
}

【讨论】:

    【解决方案2】:

    rack-rewrite 的文档在https://github.com/jtrupiano/rack-rewrite#scheme 提到了一个不错的方法

    # Redirect all https traffic to http
    r301 %r{.*}, 'http://www.example.tld$&', :scheme => 'https'
    

    【讨论】:

      【解决方案3】:

      使用 rack-rewrite 中的 scheme 选项只会导致 heroku 上的无限循环。您也不能使用 80 端口,因为 heroku 将代理您的工作人员。由于这一点以及路由层的工作方式,您必须检查 HTTP_X_FORWARDED_PROTO 标头:

      r301 %r{.*}, 'http://example.com$&', :if => Proc.new { |rack_env|
        rack_env['HTTP_X_FORWARDED_PROTO'] == 'https'
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-13
        • 2021-08-31
        • 2017-11-05
        • 1970-01-01
        • 2014-05-14
        • 2013-06-02
        • 1970-01-01
        • 2014-05-23
        • 2020-04-16
        相关资源
        最近更新 更多