开发者

redirect to root_path

开发者 https://www.devze.com 2023-03-09 16:17 出处:网络
My route开发者_StackOverflow社区 has root set to : root :to => \"posts#index\", :locale => :en

My route开发者_StackOverflow社区 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).


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消