How to redirect (301) when changed routing translation?

主宰稳场 提交于 2019-11-30 18:10:39

问题


I am looking for an easy way to make redirects in my application.

SITUATION:

I have routes like this:

http://myapp.com/authors/5-hemingway/books/1-moby-dick

The routes are translated this way (using gem 'i18n_routing'):

http://myapp.com/acutores/5-hemingway/libros/1-moby-dick

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.

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)


回答1:


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

--edit

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.



来源:https://stackoverflow.com/questions/4814063/how-to-redirect-301-when-changed-routing-translation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!