简体   繁体   English

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

[英]Why do so many Ruby on Rails apps have missing trailing slashes?

Why do so many Ruby on Rails apps have missing trailing slashes in their URLs? 为什么这么多Ruby on Rails应用程序在其URL中缺少尾部斜杠? One example is http://basecamphq.com/tour . 一个例子是http://basecamphq.com/tour AFAIK this goes against Web standards. AFAIK违反了Web标准。 Is it something to do with the way RoR is set up? 这与RoR的设置方式有关吗?

It's not against Web standards. 这不符合Web标准。 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 - eg Apache - will check the other if one doesn't exist). http://basecamphq.com/tour被认为是一个文件, http://basecamphq.com/tour/将是一个目录(注意:两个URL都不相同,尽管一些网络服务器 - 例如Apache - 会检查另一个一个不存在)。 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. 我不认为它与缓存(由nilamo提到)有关,因为有足够的HTTP标头用于缓存控制 - 可能是某些反向代理具有不同的默认行为。

Your argument is invalid: 你的论点是无效的:

w3c's url spec doesn't enforce trailing slashes on urls. w3c的url规范不会在url上强制使用尾部斜杠。

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. 通常,保留的斜杠“/”字符(ASCII 2F十六进制)表示分层结构中的级别,斜杠左侧的较高级别部分。

Rails adheres quite well to this directive. Rails非常符合这一指令。

My hair is a bird! 我的头发是一只鸟!

Because trailing slash denotes a directory, and you are not accessing directories in Rails, but pages. 因为尾部斜杠表示目录,并且您不访问Rails中的目录,而是访问页面。 It's like tour.html in your example, except that .html can be ignored as it is the default. 这就像你的例子中的tour.html,除了.html可以被忽略,因为它是默认值。

我冒昧地说,因为在RoR中,您键入的URL通常不会映射到目录中的静态文件,而是由routes.rb文件动态解析,以尾部斜杠结束路径并不会太多感。

Rails uses slashes as parameter token separators, and a route like Rails使用斜杠作为参数标记分隔符,以及类似的路径

/post/:year/:page

matches by default both, /post/2012/a-title and /post/2012/a-title/ , unless you do some magic . 默认匹配, / post / 2012 / a-title/ post / 2012 / a-title / ,除非你做了一些魔术 This has nothing to do with web standards . 这与Web标准无关

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. 在使用<img src =“ image.png ”/>回复上述内容时,浏览器将向服务器发送第二个查询: /post/2012/image.png (第一种情况)或/ post / 2012 / a- title / image.png (第二种情况),因为浏览器使用尾部斜杠来解析路径,就好像它们是目录一样。

However, Rails developers usually don't care because they don't write URLs explicitly when rendering content! 但是,Rails开发人员通常不关心,因为他们在呈现内容时不会显式写入URL! 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帮助程序来隐藏这些逻辑......除非你不使用帮助程序生成内容,否则你关心。

Some like slashes, some don't. 有些像斜线,有些则没有。 Impassioned arguments can be made for both sides. 可以为双方提出激烈的争论。

This is a form of URL Re-writing. 这是URL重写的一种形式。 It is not against web standard and actually does a lot for usability and has been proven to help your search engine rankings. 它不符合Web标准,实际上对可用性做了很多,并且已被证明可以帮助您的搜索引擎排名。 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 http://www.coolwebsite.com/post.aspx?id=aebe6ca7-6c65-4b5c-bac8-9849faa0a467

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM