我需要使http://example.com轉到https://www.example.com。現在它在瀏覽器中發出警告。我跟着:http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/如何在force_ssl之前執行301? Rails 3.1
從/ lib目錄/中間件負載(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
生產ENV
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
只是Gem'rack-ssl''在Gemfile中爲我工作 –