views:

62

answers:

2

I created a new website, 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?

+1  A: 

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.

Jim
No worries, thanks!
LDK
This looks much cleaner. I suppose I should put that in an unless statement to ensure I'm not redirecting people, or more importantly Google bots, going to the newdomain...
LDK
+1  A: 

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.

fractious
Cool, thanks for the tip.
LDK