hoffm
hoffm

Reputation: 2436

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.

Upvotes: 6

Views: 754

Answers (2)

Michaël Witrant
Michaël Witrant

Reputation: 7714

match '/articles(/*path)' => redirect { |params, req| "/blog/#{params[:path]}" }

Upvotes: 1

felipeclopes
felipeclopes

Reputation: 4070

Edit

The problem was catching the routes with parameters, now it should be solved

Following the rails guides, the way to do it should be:

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

Extracted from Redirection

I hope this helps you!

Upvotes: 0

Related Questions