如何在force_ssl之前执行301? Rails 3.1 [英] How to perform 301 before force_ssl? Rails 3.1

查看:48
本文介绍了如何在force_ssl之前执行301? Rails 3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 http://example.com 转到 http://www.simonecarletti.com /blog/2011/05/configuring-rails-3-https-ssl/

I need to make http://example.com go to https://www.example.com. right now it's warning in the browser. I followed: http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/

从/lib/middleware加载(Rails 3.1)

loading from /lib/middleware (Rails 3.1)

class WwwMiddlewareRedirect
  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    if request.host.starts_with?("example.com")
      [301, {"Location" => request.url.sub("//", "//www.")}, self]
    else
      @app.call(env)
    end
  end

  def each(&block)
  end
end

生产环境

Example::Application.configure do

  config.force_ssl = true

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  config.middleware.use "WwwMiddlewareRedirect"
end

推荐答案

如果可以让您的Web服务器执行重定向,那通常是最好的.如果您使用的是Heroku或类似的平台,但无法让Web服务器为您重定向,请进行以下更改:

If you can get your webserver to do the redirect, that's usually best. If you're on Heroku or a similar platform where you can't get the webserver to redirect for you, make these changes:

将中间件行更改为:

config.middleware.insert_before Rack::SSL, "WwwMiddlewareRedirect"

然后在您的gemfile中,包括:

Then in your gemfile, include:

gem 'rack-ssl', :require => 'rack/ssl'

这篇关于如何在force_ssl之前执行301? Rails 3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆