【网络基础】URI 和 URL 的纠缠

来源:互联网 发布:php pregsplit 编辑:程序博客网 时间:2024/05/02 01:52

名词解释:

URI (uniform resource identifier) 统一资源标志符;
URL(uniform resource location ) 统一资源定位符(或统一资源定位器);
URN(uniform resource name ) 统一资源命名。

URN和URL是URI的子集

这里写图片描述

背景

有很多经典的技术争论,而如何正确的称呼web地址是最难以区分的问题之一,比如: 某人说:给我个URL,这个时候另外一位”高人”会跳出来纠正:其实这叫URI,不是URL.

从官方描述开始

通览一下RFC及wikipedia 的官方描述:

A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource.

URI是一组紧凑的字符序列,用来唯一标识一个抽象或者实际的资源

A URI can be further classified as a locator, a name, or both. The term “Uniform Resource Locator” (URL) refers to the subset of URIs that, in addition to identifying a resource, provide a means of locating the resource by describing its primary access mechanism (e.g., its network “location”)

URI更细致的分类的话,有多种具体形式,locator, 一个名称,或者二者皆包括, URL 是URI的子集,但是为了进一步定位一个资源,URL借助定义资源的访问机制来提供一种资源定位的方式,比如网络资源位置

One can classify URIs as locators (URLs), or as names (URNs), or as both. A Uniform Resource Name (URN) functions like a person’s name, while a Uniform Resource Locator (URL) resembles that person’s street address. In other words: the URN defines an item’s identity, while the URL provides a method for finding it.

一个URI可以归为URL 或者 URN,有的URL即是URL也是URN,类比一下 URN更像是一个人的名字,而URL则类似某个人所在的具体位置。再换个角度来说,URN定义了资源的唯一标识符,而URL则提供了如何找到该资源的方法。

由上描述可知

1.一个URI之所以是URL的关键就是: 指定了 “访问机制”或 “网络地址”比如出现“http://”或者 “ftp://”
2.URN是指 资源的唯一的名称。

看看例子

  • ftp://ftp.is.co.za/rfc/rfc1808.txt (also a URL because of the protocol)
  • http://www.ietf.org/rfc/rfc2396.txt (also a URL because of the protocol)
  • ldap://[2001:db8::7]/c=GB?objectClass?one (also a URL because of the protocol)
  • mailto:John.Doe@example.com (also a URL because of the protocol)
  • news:comp.infosystems.www.servers.unix (also a URL because of the protocol)
  • tel:+1-816-555-1212
  • telnet://192.0.2.16:80/ (also a URL because of the protocol)
  • urn:oasis:names:specification:docbook:dtd:xml:4.1.2

再强调一下,什么是URL ,就是告诉你”如何去找到他们”

那如何根据给出的URI正确的称呼呢?

这取决于一个条件:就是所给出的一个URI是完整,抑或只是一部分。
比如:你需要的一个文件位于files.hp.com,这就是一个URI,非URL,访问协议及端口都没有。http://files.hp.com 则是一个URL.

结论

叫URI永远没错,但并不是最精确

  • 关于某个资源,仅仅只有名称或者某些不完整信息,那么称呼URI
  • 但如果即给出了资源名称又提供了访问资源的方法,那么称呼URL更精确。

URI 是定位符,它可以是名称,位置信息,也可能二者兼备

URN,URL是URI,反之则不对

之所以为URL 是因为提供了资源名称及资源访问方法

参考

https://danielmiessler.com/study/url-uri/

0 0