0

我希望 /page/index 重定向到 /page/ 但是

get '/page/index' => redirect('/page/')

不管用。我是否必须在索引控制器中使用 redirect_to (我想避免它,因为它会导致页面的完整请求/重新加载)?同样,我注意到 rails 将 /page 和 /page/ 视为两个不同的 url,如何将 /page 重定向到 /page/ ?作为

get '/page' => redirect('/page/') 

也不起作用(它给了我无限的重定向问题)。

4

1 回答 1

0

If you don't want to cause a full reload of the page, then you should not use "redirect" in your routes, as that simply issues a browser redirect. That would cause Rails to issue a 301 "Moved Permanently". That essentially means "there is a /page/index route but it's a mistake, we don't use it anymore, please go to /page instead and never come back here." If that is the case then that is what you want to do. If you're using Passenger and will continue to use Passenger, it would be more efficient to put an Apache redirect rule in the htaccess file.

It also seems strange that this is even an issue. How have /page/index, /page, and /page/ emerged in the wild as URLs that all point to the same index#index action? What is this index controller? The usual thing is to have, say, a PagesController that has your home page at index action. Then:

root to: 'pages#index"

and

<%= root_url %>

and

redirect_to root_path

Always go to the correct path and you don't have to worry about trailing slashes and such.

于 2013-06-09T17:13:02.493 回答