我想知道是否有 Rack 替代方案来强制 URL 中的“www”,因为 Heroku 不使用 .htaccess 文件。
也许甚至是在 route 做到这一点的好方法?
谢谢
最佳答案
在您的 ApplicationController 中,您可以简单地创建一个 before 过滤器:
before_filter :force_www!
protected
def force_www!
if Rails.env.production? and request.host[0..3] != "www."
redirect_to "#{request.protocol}www.#{request.host_with_port}#{request.fullpath}", :status => 301
end
end
或者转到另一个方向并删除 www:
before_filter :remove_www!
protected
def remove_www!
if Rails.env.production? and request.host[0..3] == "www."
redirect_to "#{request.protocol}#{request.host_with_port[4..-1]}#{request.fullpath}", :status => 301
end
end
关于ruby-on-rails-3 - 在没有 .htaccess 的情况下在 Heroku 上托管的 Rails3 中强制 'www',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905664/