如何在更改路由转换时重定向(301)?
我正在寻找一种简单的方法来在我的应用程序中进行重定向。
情况:
我有这样的路线:
http://myapp.com/authors/5-hemingway/books/1-moby-dick
路线以这种方式翻译(使用gem’i18n_routing’):
http://myapp.com/acutores/5-hemingway/libros/1-moby-dick
现在,我将acutores的翻译改为了scriptores。 简单的步骤,但我想将包含旧“acutores”资源名称的所有路由重定向到带有“scriptores”的路由。
我的猜测是,我应该在routes.rb中玩:
match "/acutores" => redirect("/scriptores")
但是如何在“acutores”出现的所有情况下有效地做到这一点? (尤其是嵌套路线)
这会重定向/acutores/something
到/acutores/something
但是使用plain /acutores
失败:
match "/acutores/*path" => redirect("/scriptores/%{path}")
这似乎同时处理:
match "/acutores(/*path)" => redirect {|params| "/scriptores/#{params[:path]}"}
- http://guides.rubyonrails.org/routing.html#redirection
- http://guides.rubyonrails.org/routing.html#route-globbing
– 编辑
这将删除所有尾部斜杠:
match "/acutores(/*path)" => redirect{ |params| "/scriptores/#{params[:path]}".chomp("/") }
我遇到了浏览器缓存重定向的问题,因此在修改后清空缓存。