Rails 应用程序中的尾部斜杠行为
我目前正在尝试使用类别/文章模式模仿 Rails 中的文件夹/文件行为。所以,我在路线中有这个:
match '/:category/' => 'category#list_articles'
match '/:category/:article.:format' => 'article#show'
基本上,请求网址是:
http://www.example.com/category/
http://www.example.com/category/article.html
一切正常。问题是它运行得很好。像 http://www.example.com/category
这样的 URL(不带尾部斜杠)也提供文章列表。是否存在一种方法可以使用 404 来阻止此行为,或者更好地重定向到带有尾部斜杠的类别?
使用 Rails 3、nginx、ruby 1.9.2。谢谢!
I am currently trying to mimic folder/files behavior in rails with category/articles schema. So, I have this in routes :
match '/:category/' => 'category#list_articles'
match '/:category/:article.:format' => 'article#show'
Basically, request urls are :
http://www.example.com/category/
http://www.example.com/category/article.html
Everything works. The problem is it's working to well. An url like http://www.example.com/category
(without the trailing slash) serves also the list of articles. Does it exist an a way either to block this with a 404 or better to redirect to the category with the trailing slash ?
Using Rails 3, nginx, ruby 1.9.2. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 Rails 中是否有东西可以为您做到这一点,但这应该可以:
I'm not sure there isn't something in rails that does it for you, but this should do: