Site wide redirect
I created a new websit开发者_如何学Ce, hosted with Heroku, about 3 weeks ago and have since decided I wanted to change its domain name. I was trying to figure out how to do a site wide redirect and I'm thinking of going with something like in my application controller (add a before filter):
def new_domain
redirect_to url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :host => 'http://newdomain.com'), :status => 301
end
This obviously will not work with all actions and will fail if the url is passing additional parameters. I was wondering if there is something more general I can do so I don't have to have a bunch of if else clauses?
Try this.
params[:host] = 'http://newdomain.com'
redirect_to url_for(params), :status => 301
'url_for' take a hash and by changing the 'host' in the params, it should go to the new host with the rest of the params.
As recent versions of Rails now adhere to the Rack interface and can incorporate Rack middleware it might be worth taking a look at Canonical Host. I haven't used it myself but at first glance it sounds like it might be nice solution.
精彩评论