如何在没有Rails首先剥离变量的情况下,在routes.rb中使用GET变量重定向URL?

Mic*_*ael 1 rack routes ruby-on-rails http-status-code-301 ruby-on-rails-3

我正在Rails中建立一个网站来取代现有的网站.在routes.rb我试图将一些旧的URL重定向到他们的新等价物(一些URL slugs正在改变,所以动态解决方案是不可能的.)

routes.rb看起来像这样:

  match "/index.php?page=contact-us" => redirect("/contact-us")
  match "/index.php?page=about-us" => redirect("/about-us")
  match "/index.php?page=committees" => redirect("/teams")
Run Code Online (Sandbox Code Playgroud)

当我访问时,/index.php?page=contact-us我没有被重定向到/contact-us.我已经确定这是因为Rails正在删除get变量而只是尝试匹配/index.php.例如,如果我/index.php?page=contact-us进入以下路线,我将被重定向到/foobar:

  match "/index.php?page=contact-us" => redirect("/contact-us")
  match "/index.php?page=about-us" => redirect("/about-us")
  match "/index.php?page=committees" => redirect("/teams")
  match "/index.php" => redirect("/foobar")
Run Code Online (Sandbox Code Playgroud)

如何将GET变量保留在字符串中并按照我喜欢的方式重定向旧URL?Rails是否有预期的机制?

Waw*_*Loo 5

我有类似的情况,这就是我做的:

创建一个控制器,通过一个操作来处​​理重定向

RedirectController < ApplicationController
  def redirect_url
    if params[:page]
      redirect_to "/#{params[:page]}", :status => 301
    else
      raise 404 #how handle your 404
  end
end
Run Code Online (Sandbox Code Playgroud)

在routes.rb中

match "/index.php" => "redirect#redirect_url"
Run Code Online (Sandbox Code Playgroud)