301 redirecting a group of related paths in Rails 3 routes

I want to set up a constant redirect of each path starting with /articles to a similar path starting with /blog .

I know how to redirect paths individually in routes, for example.

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

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

I hope there is a way to redirect all such paths, present and future, in one fell swoop.

I understand that I could do this relatively easily in the controller with before_filter , but I believe that this behavior applies to routes, and I hope to keep it there.

+6
source share
2 answers
 match '/articles(/*path)' => redirect { |params, req| "/blog/#{params[:path]}" } 
+1
source

Edit

The problem was catching routes with parameters, now they need to be solved

Following the guides along the guides, a way to do this should be:

 match "/articles(/:action(/:id(.:format)))" => redirect {|p, req| "/blog/#{req.subdomain}" } 

Extracted from Redirection

Hope this helps you!

0
source

All Articles