Reputation: 33
I was wondering if there was a Rack alternative to forcing the 'www' in the URL since Heroku doesn't use .htaccess files.
Maybe even a nice way to do it in routes?
Thanks
Upvotes: 3
Views: 1934
Reputation: 16345
A quick Google search reveals this Rack middleware, which appears to do exactly what you want.
Upvotes: 2
Reputation: 13263
In your ApplicationController, you can simply create a before filter:
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
Or to go the other direction and remove 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
Upvotes: 11