Manish Shrivastava
Manish Shrivastava

Reputation: 32040

In Rails , Redirect old url with 'renamed params' to new url

My application Old url was (in php)

www.abc.com/athlete/?country=2

and now new url is (In Rails):

www.asdf.com/athletes/?c=2

Now, I need to redirect (google crawls) old url to new url. Either via config/routes.rb or via redirect in controller using redirecting one vertual action to

def redirect_to_index
   redirect_to :action => :index, :c => params[:country], :status => 301
end

But problem is there are many old urls are without params i.e. no country. but on reidrecting I will have ?c= always.

I don't want to use rough code like

def redirect_to_index
   if params[:country]
     redirect_to :action => :index, :c => params[:country], :status => 301
   else
    redirect_to :action => :index, :status => 301
   end
end

Any better solution?

Upvotes: 1

Views: 511

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54674

You could do something like this:

new_params = {
  :c => params[:country],
  :x => params[:something_else]
}.reject{|k,v| v.nil? }

redirect_to ({ :action => :index, :status => 301 }.merge(new_params))

Upvotes: 2

Related Questions