问题描述
我正在寻找一种在应用程序中进行重定向的简单方法。
I am looking for an easy way to make redirects in my application.
情况:
I有这样的路线:
http://myapp.com/authors/5-hemingway/books/1-moby-dick
以这种方式翻译路线(使用gem'i18n_routing'):
The routes are translated this way (using gem 'i18n_routing'):
http://myapp.com/acutores/5-hemingway/libros/1-moby-dick
现在,我将acutores的翻译更改为scriptores。简单的步骤,但是我想将所有包含旧的 acutores资源名称的路由重定向到带有 scriptores的路由。
Now, I changed translation of acutores to scriptores. Easy step but I'd like to redirect all routes that contained an old "acutores" resource name to routes with "scriptores" instead.
我的猜测是,我应该玩在route.rb中,使用以下命令:
My guess is, I should play in routes.rb with:
match "/acutores" => redirect("/scriptores")
但是如何有效地解决出现委托人的所有情况? (尤其是嵌套路由)
But how to do it efficiently for all cases where 'acutores' appear? (especially with nested routes)
推荐答案
这将重定向 / acutores / something
到 / scriptores / something
,但失败并显示 / acutores
:
This redirects /acutores/something
to /scriptores/something
but fails with plain /acutores
:
match "/acutores/*path" => redirect("/scriptores/%{path}")
这似乎可以处理这两个问题:
This seems to handle both:
match "/acutores(/*path)" => redirect {|params| "/scriptores/#{params[:path]}"}
- http://guides.rubyonrails.org/routing.html#redirection
- http://guides.rubyonrails.org/routing.html#route-globbing
-编辑
这将消除所有尾随的斜杠:
This will get rid of all the trailing slashes:
match "/acutores(/*path)" => redirect{ |params| "/scriptores/#{params[:path]}".chomp("/") }
我在浏览器缓存重定向方面遇到问题,因此请在修改后清空缓存。
I had issues with browser caching redirects, so empty the cache after modifications.
这篇关于更改路由转换后如何重定向(301)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!