berkes
berkes

Reputation: 27543

redirect to root_path

My route has root set to :

root :to => "posts#index", :locale => :en

But /en/posts/ and /en/posts/index is still accessible, showing "duplicate" content from the root_path. I would like to redirect to root_path, if one of those paths is used.

That way, I can avoid duplicate content in search engines and my users have a more consistent experience: a resource lives on only one place and one place only (REST).

Upvotes: 1

Views: 2172

Answers (2)

Peter Giacomo Lombardo
Peter Giacomo Lombardo

Reputation: 712

I recently found a solution to this issue by using the following in my routes.rb:

get "/en/posts" => redirect("/")
resources :posts
root :to => "posts#index", :locale => :en

Requests to /en/posts gets 301 redirected (Moved permanently) to the root path

Upvotes: 0

Matthew Lehner
Matthew Lehner

Reputation: 4027

You can turn off the posts/index url by editing the posts resource in your routes.rb file to look like this:

resources :posts, :except => :index

If this gives you issues with redirection, read the rails guides on routing, specifically section 3.12 Redirection

Upvotes: 1

Related Questions