问题描述
我需要将 http://example.com 转到 https://www.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!