为什么这么多 Ruby on Rails 应用程序缺少尾部斜杠?
为什么这么多 Ruby on Rails 应用程序的 URL 中缺少尾部斜杠?一个例子是 http://basecamphq.com/tour。 AFAIK 这违背了网络标准。这与 RoR 的设置方式有关吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它并不违反 Web 标准。 http://basecamphq.com/tour 被视为文件,http://basecamphq.com/tour/ 将是一个目录(注意:两个 URL 不相等,尽管某些网络服务器 - 例如 Apache - 会检查另一个(如果不存在)。由于两者都是虚拟的,因此主要由开发人员决定(这与所使用的编程语言或框架无关)。
我不认为它与缓存有关(如 nilamo 提到的),因为有足够的 HTTP 标头用于缓存控制 - 但可能某些反向代理具有不同的默认行为。
It's not against Web standards. http://basecamphq.com/tour is considered a file, http://basecamphq.com/tour/ would be a directory (Note: both URLs aren't equal, although some webservers - e.g. Apache - will check the other if one doesn't exist). As both are kind of virtual, it's mainly up to the developer to decide (this is independent of used programming languages or frameworks).
I don't think it has something to do with caching (as mentioned by nilamo) as there are enough HTTP headers for cache control - might be that some reverse proxies have different default behavior though.
您的参数无效:
w3c 的 url 规范 不强制使用尾部斜杠在网址上。
这是关于斜线的说法:
Rails 很好地遵守了该指令。
我的头发是一只鸟!
Your argument is invalid:
w3c's url spec doesn't enforce trailing slashes on urls.
This is what it says about slashes:
Rails adheres quite well to this directive.
My hair is a bird!
因为尾部斜杠表示一个目录,并且您访问的不是 Rails 中的目录,而是页面。它就像您的示例中的tour.html,只不过 .html 可以被忽略,因为它是默认值。
Because trailing slash denotes a directory, and you are not accessing directories in Rails, but pages. It's like tour.html in your example, except that .html can be ignored as it is the default.
我敢说,由于在 RoR 中,您键入的 URL 通常不会映射到目录中的静态文件,而是由 paths.rb 文件动态解析,因此以尾部斜杠结束路径并没有多大作用感觉。
I'd venture to say that since in RoR, the URL you type usually does not map to a static file in a directory, but is resolved dynamically by the routes.rb file, ending the path with a trailing slash doesn't make much sense.
有些人喜欢斜线,有些则不喜欢。双方都可以进行激烈的争论。
Some like slashes, some don't. Impassioned arguments can be made for both sides.
类似的路由
Rails 使用斜杠作为参数标记分隔符,默认情况下, 会匹配 /post/2012/a-title 和 /post/2012/a-title/,除非你做了一些魔法。 这与网络标准无关。
从浏览器的角度来看,这两种路径在处理相关资源时有很大不同。在对上述内容的回应中,使用 浏览器将向服务器发送第二个查询:/post/2012/image.png(第一种情况)或/post/2012/a-title/image.png > (第二种情况),因为浏览器使用尾部斜杠来解析路径,就好像它们是目录一样。
然而,Rails 开发人员通常并不关心,因为他们在渲染内容时不会显式地编写 URL!他们有可供使用的 URL 帮助程序,可以向他们隐藏此逻辑...除非您不使用帮助程序来生成内容,否则您会关心。
Rails uses slashes as parameter token separators, and a route like
matches by default both, /post/2012/a-title and /post/2012/a-title/, unless you do some magic. This has nothing to do with web standards.
From the point of view of the browser, these two paths are very different when it comes to deal with relative resources. In a response to the above with <img src="image.png"/> the browser will send a second query to the server for: /post/2012/image.png (first case) or /post/2012/a-title/image.png (second case), because the browser uses the trailing slash to resolve paths as if they were directories.
However, Rails developers usually don't care because they don't write URLs explicitly when rendering content! They have at their disposal URL helpers which hide this logic from them... unless you don't use the helpers to generate content, then you care.
这是 URL 重写的一种形式。它并不违反网络标准,实际上在可用性方面做了很多工作,并且已被证明可以帮助您的搜索引擎排名。这样想吧。
您正在告诉您的朋友您在某人的博客上看到的这篇很酷的帖子。哪个 URL 更容易告诉您的朋友:
或
This is a form of URL Re-writing. It is not against web standard and actually does a lot for usability and has been proven to help your search engine rankings. Think of it this way.
You are telling your friend about this cool post you seen on someone's blog. Which URL is easier to tell your friend:
OR