我的应用程序旧URL是(在php)
www.abc.com/athlete/?country=2
现在新URL是(在Rails中):
www.asdf.com/athletes/?c=2
现在,我需要将旧URL重定向到新URL。通过config/routes.rb
或通过将一个Vertual动作重定向到
def redirect_to_index
redirect_to :action => :index, :c => params[:country], :status => 301
end
但是问题是有许多旧的URL没有参数,即无country
。但是在重新效果时,我将始终有?c=
。
我不想使用
之类的粗糙代码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
有更好的解决方案?
您可以做这样的事情:
new_params = {
:c => params[:country],
:x => params[:something_else]
}.reject{|k,v| v.nil? }
redirect_to ({ :action => :index, :status => 301 }.merge(new_params))