![](/img/trans.png)
[英]Rails: How to build a url with redirect_to and routes.rb?
[英]How to redirect a URL with GET variables in routes.rb without Rails stripping out the variable first?
我正在Rails中建立一個網站來替換現有網站。 在routes.rb
我嘗試將一些舊URL重定向到它們的新等效項(某些URL標記正在更改,因此無法采用動態解決方案。)
我的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")
當我訪問/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")
如何將GET變量保留在字符串中,並按照自己的方式重定向舊的URL? Rails是否有針對性的機制?
我有類似的情況,這就是我所做的:
創建一個控制器,通過一個動作處理重定向
RedirectController < ApplicationController
def redirect_url
if params[:page]
redirect_to "/#{params[:page]}", :status => 301
else
raise 404 #how handle your 404
end
end
在routes.rb中
match "/index.php" => "redirect#redirect_url"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.