Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 redirect of a group of related paths in Rails 3 routes

I want to set up a permanent redirect of every path that begins with /articles to the analogous path that begins with /blog.

I know how to redirect paths individually in the routes, e.g.

match "/articles/" => redirect("/blog/")

However, if I also want to redirect paths such as /articles/:id and /articles/category/:id etc., I'll need to have explicit redirects for those as well.

I'm hoping that there's a way to redirect all such paths, present and future, with one fell swoop.

I realize that I could do this relatively easily in the controller with a before_filter, but I believe this behavior belongs in the routes and I'm hoping to keep it there.

like image 551
hoffm Avatar asked Dec 20 '12 19:12

hoffm


1 Answers

match '/articles(/*path)' => redirect { |params, req| "/blog/#{params[:path]}" }
like image 162
Michaël Witrant Avatar answered Oct 25 '22 06:10

Michaël Witrant