sc0*_*man 3 routing rack ruby-on-rails-3
我想知道是否有一个Rack替代强制URL中的'www',因为Heroku不使用.htaccess文件.
也许甚至可以在路线上做到这一点?
谢谢
Adr*_*eil 11
在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
Run Code Online (Sandbox Code Playgroud)
或者去另一个方向并删除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
Run Code Online (Sandbox Code Playgroud)