【问题标题】:In Rails , Redirect old url with 'renamed params' to new url在 Rails 中,将带有“重命名参数”的旧网址重定向到新网址
【发布时间】:2014-02-12 03:43:13
【问题描述】:

我的应用程序旧网址是(在 php 中)

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

现在新的网址是(在 Rails 中):

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

现在,我需要将旧网址重定向(谷歌抓取)到新网址。通过config/routes.rb 或通过控制器中的重定向,使用将一个 vertual 操作重定向到

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

但问题是有很多旧网址没有参数,即没有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

有更好的解决方案吗?

【问题讨论】:

  • 最好如果routes.rb可以处理match "/athlete/?country=:country" => redirect("/athlete/?c=:country")这样的情况

标签: ruby-on-rails ruby redirect routes


【解决方案1】:

你可以这样做:

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

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

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多