且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么时候应该在网址中使用斜杠?

更新时间:2023-11-08 20:21:10

在我个人看来,尾部的斜杠被滥用了.

基本上,URL格式来自相同的UNIX格式的文件和文件夹,此后在DOS系统上,最后适用于Web.

在类似Unix的操作系统上,这本书的典型URL是诸如file:///home/username/RomeoAndJuliet.pdf之类的文件路径,用于标识保存在本地硬盘上文件中的电子书.

来源:***:统一资源标识符

另一个值得阅读的好资源:***:URI方案

根据RFC 1738(在1994年定义了URL),当资源包含对其他资源的引用时,它们可以使用相对链接来定义第二个资源的位置,就好比说与该资源位于同一位置,除了以下相对路径".继续说,这样的相对URL依赖于原始URL,该URL包含相对链接所基于的层次结构,并且ftp,http, 文件URL方案和文件URL方案是可以视为分层结构的示例,分层结构的各个组成部分之间用"/"分隔.

来源:***统一资源定位符(URL)

也:

这是我们经常听到的问题.继续回答!从历史上看,带有斜杠的URL表示目录和不带斜杠的URL代表目录是很常见的 表示一个文件:

http://example.com/foo/(带斜杠,通常是目录)>

http://example.com/foo (不带斜杠,通常是文件)

来源: Google WebMaster Central博客-至斜线还是不斜线

最后:

  1. URL末尾的斜杠使地址看起来漂亮".

  2. 在末尾没有斜杠且没有扩展名的URL看起来有些怪异".

  3. 您将永远不会为CSS文件命名(例如) http://www.sample.com/stylesheet/您愿意吗?

但是无论环境如何,我都是Web***实践的拥护者. 就像您所说的不带扩展名的URL一样,它可能是眨眼而不清楚的.

When should a trailing slash be used in a URL? For example - should my URL look like /about-us/ or like /about-us?

I am fully aware of the SEO-related issues - duplicate content and the canonical thing; I'm trying to figure out which one I should use in the context of serving pages correctly alone.

For example, my colleague is thinking that a trailing slash at the end means it's a "folder" - a "directory", so this is not a correct style. But I think that without a slash in the end - it's not quite correct either, because it almost looks like a folder, but it isn't and it's not a normal file either, but a filename without extension.

Is there a proper way of knowing which to use?

In my personal opinion trailing slashes are misused.

Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.

A typical URL for this book on a Unix-like operating system would be a file path such as file:///home/username/RomeoAndJuliet.pdf, identifying the electronic book saved in a file on a local hard disk.

Source: Wikipedia: Uniform Resource Identifier

Another good source to read: Wikipedia: URI Scheme

According to RFC 1738, which defined URLs in 1994, when resources contain references to other resources, they can use relative links to define the location of the second resource as if to say, "in the same place as this one except with the following relative path". It went on to say that such relative URLs are dependent on the original URL containing a hierarchical structure against which the relative link is based, and that the ftp, http, and file URL schemes are examples of some that can be considered hierarchical, with the components of the hierarchy being separated by "/".

Source: Wikipedia Uniform Resource Locator (URL)

Also:

That is the question we hear often. Onward to the answers! Historically, it’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file:

http://example.com/foo/ (with trailing slash, conventionally a directory)

http://example.com/foo (without trailing slash, conventionally a file)

Source: Google WebMaster Central Blog - To slash or not to slash

Finally:

  1. A slash at the end of the URL makes the address look "pretty".

  2. A URL without a slash at the end and without an extension looks somewhat "weird".

  3. You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?

BUT I'm being a proponent of web best practices regardless of the environment. It can be wonky and unclear, just as you said about the URL with no ext.