为什么这么多 Ruby on Rails 应用程序缺少尾部斜杠?

发布于 2024-08-08 12:35:13 字数 178 浏览 6 评论 0 原文

为什么这么多 Ruby on Rails 应用程序的 URL 中缺少尾部斜杠?一个例子是 http://basecamphq.com/tour。 AFAIK 这违背了网络标准。这与 RoR 的设置方式有关吗?

Why do so many Ruby on Rails apps have missing trailing slashes in their URLs? One example is http://basecamphq.com/tour. AFAIK this goes against Web standards. Is it something to do with the way RoR is set up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

神妖 2024-08-15 12:35:13

它并不违反 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.

遗心遗梦遗幸福 2024-08-15 12:35:13

您的参数无效:

w3c 的 url 规范 不强制使用尾部斜杠在网址上。

这是关于斜线的说法:

路径的解释方式
取决于所使用的方案。
一般保留斜杠“/”
字符(ASCII 2F 十六进制)表示
在层级结构中,层级
左侧的更高级别部分
斜杠。

Rails 很好地遵守了该指令。

我的头发是一只鸟!

Your argument is invalid:

w3c's url spec doesn't enforce trailing slashes on urls.

This is what it says about slashes:

The path is interpreted in a manner
dependent on the scheme being used.
Generally, the reserved slash "/"
character (ASCII 2F hex) denotes a
level in a hierarchical structure, the
higher level part to the left of
the slash.

Rails adheres quite well to this directive.

My hair is a bird!

只涨不跌 2024-08-15 12:35:13

因为尾部斜杠表示一个目录,并且您访问的不是 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.

追我者格杀勿论 2024-08-15 12:35:13

我敢说,由于在 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.

在巴黎塔顶看东京樱花 2024-08-15 12:35:13

有些人喜欢斜线,有些则不喜欢。双方都可以进行激烈的争论。

Some like slashes, some don't. Impassioned arguments can be made for both sides.

萌逼全场 2024-08-15 12:35:13

类似的路由

/post/:year/:page

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

/post/:year/:page

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.

三人与歌 2024-08-15 12:35:13

这是 URL 重写的一种形式。它并不违反网络标准,实际上在可用性方面做了很多工作,并且已被证明可以帮助您的搜索引擎排名。这样想吧。

您正在告诉您的朋友您在某人的博客上看到的这篇很酷的帖子。哪个 URL 更容易告诉您的朋友:

  1. http:// /www.coolwebsite.com/post.aspx?id=aebe6ca7-6c65-4b5c-bac8-9849faa0a467

  1. http://www.coolwebsite.com/cool-ideas-for-posts/

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:

  1. http://www.coolwebsite.com/post.aspx?id=aebe6ca7-6c65-4b5c-bac8-9849faa0a467

OR

  1. http://www.coolwebsite.com/cool-ideas-for-posts/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文