What is the difference between a URI, a URL and a URN?

前端 未结 30 1258
小蘑菇
小蘑菇 2020-11-22 02:18

People talk about URLs, URIs, and URNs as if they\'re different things, but they look the same to the naked eye.

W

相关标签:
30条回答
  • A small addition to the answers already posted, here's a Venn's diagram to sum up the theory (from Prateek Joshi's beautiful explanation):

    enter image description here

    And an example (also from Prateek's website):

    enter image description here

    0 讨论(0)
  • 2020-11-22 02:52

    In summary: a URI identifies, a URL identifies and locates.

    Consider a specific edition of Shakespeare's play Romeo and Juliet, of which you have a digital copy on your home network.

    You could identify the text as urn:isbn:0-486-27557-4.
    That would be a URI, but more specifically a URN* because it names the text.

    You could also identify the text as file://hostname/sharename/RomeoAndJuliet.pdf.
    That would also be a URI, but more specifically a URL because it locates the text.

    *Uniform Resource Name

    (Note that my example is adapted from Wikipedia)

    0 讨论(0)
  • 2020-11-22 02:52

    URL

    A URL is a specialization of URI that defines the network location of a specific resource. Unlike a URN, the URL defines how the resource can be obtained. We use URLs every day in the form of http://example.com etc. But a URL doesn't have to be an HTTP URL, it can be ftp://example.com etc., too.

    URI

    A URI identifies a resource either by location, or a name, or both. More often than not, most of us use URIs that defines a location to a resource. The fact that a URI can identify a resources by both name and location has lead to a lot of the confusion in my opinion. A URI has two specializations known as URL and URN.

    Difference between URL and URI

    A URI is an identifier for some resource, but a URL gives you specific information as to obtain that resource. A URI is a URL and as one commenter pointed out, it is now considered incorrect to use URL when describing applications. Generally, if the URL describes both the location and name of a resource, the term to use is URI. Since this is generally the case most of us encounter everyday, URI is the correct term.

    0 讨论(0)
  • 2020-11-22 02:55

    URI -- Uniform Resource Identifier

    URIs are a standard for identifying documents using a short string of numbers, letters, and symbols. They are defined by RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax. URLs, URNs, and URCs are all types of URI.

    URL -- Uniform Resource Locator

    Contains information about how to fetch a resource from its location. For example:

    • http://example.com/mypage.html
    • ftp://example.com/download.zip
    • mailto:user@example.com
    • file:///home/user/file.txt
    • tel:1-888-555-5555
    • http://example.com/resource?foo=bar#fragment
    • /other/link.html (A relative URL, only useful in the context of another URL)

    URLs always start with a protocol (http) and usually contain information such as the network host name (example.com) and often a document path (/foo/mypage.html). URLs may have query parameters and fragment identifiers.

    URN -- Uniform Resource Name

    Identifies a resource by a unique and persistent name, but doesn't necessarily tell you how to locate it on the internet. It usually starts with the prefix urn: For example:

    • urn:isbn:0451450523 to identify a book by its ISBN number.
    • urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66 a globally unique identifier
    • urn:publishing:book - An XML namespace that identifies the document as a type of book.

    URNs can identify ideas and concepts. They are not restricted to identifying documents. When a URN does represent a document, it can be translated into a URL by a "resolver". The document can then be downloaded from the URL.

    URC -- Uniform Resource Citation

    Points to meta data about a document rather than to the document itself. An example of a URC is one that points to the HTML source code of a page like: view-source:http://example.com/

    Data URI

    Rather than locating it on the internet, or naming it, data can be placed directly into a URI. An example would be data:,Hello%20World.


    Frequently Asked Questions

    I've heard that I shouldn't say URL anymore, why?

    The W3 spec for HTML says that the href of an anchor tag can contain a URI, not just a URL. You should be able to put in a URN such as <a href="urn:isbn:0451450523">. Your browser would then resolve that URN to a URL and download the book for you.

    Do any browsers actually know how to fetch documents by URN?

    Not that I know of, but modern web browser do implement the data URI scheme.

    Does the difference between URL and URI have anything to do with whether it is relative or absolute?

    No. Both relative and absolute URLs are URLs (and URIs.)

    Does the difference between URL and URI have anything to do with whether it has query parameters?

    No. Both URLs with and without query parameters are URLs (and URIs.)

    Does the difference between URL and URI have anything to do with whether it has a fragment identifier?

    No. Both URLs with and without fragment identifiers are URLs (and URIs.)

    Does the difference between URL and URI have anything to do with what characters are permitted?

    No. URLs are defined to be a strict subset of URIs. If a parser allows a character in a URL but not in a URI, there is a bug in the parser. The specs go into great detail about which characters are allowed in which parts of URLs and URIs. Some characters may be allowed only in some parts of the URL, but characters alone are not a difference between URLs and URIs.

    But doesn't the W3C now say that URLs and URIs are the same thing?

    Yes. The W3C realized that there is a ton of confusion about this. They issued a URI clarification document that says that it is now OK to use the terms URL and URI interchangeably (to mean URI). It is no longer useful to strictly segment URIs into different types such as URL, URN, and URC.

    Can a URI be both a URL and a URN?

    The definition of URN is now looser than what I stated above. The latest RFC on URIs says that any URI can now be a URN (regardless of whether it starts with urn:) as long as it has "the properties of a name." That is: It is globally unique and persistent even when the resource ceases to exist or becomes unavailable. An example: The URIs used in HTML doctypes such as http://www.w3.org/TR/html4/strict.dtd. That URI would continue to name the HTML4 transitional doctype even if the page on the w3.org website were deleted.


    URI/URL Venn Diagram

    0 讨论(0)
  • 2020-11-22 02:57

    Another example I like to use when thinking about URIs is the xmlns attribute of an XML document:

    <rootElement xmlns:myPrefix="com.mycompany.mynode">
        <myPrefix:aNode>some text</myPrefix:aNode>
    </rootElement>
    

    In this case com.mycompany.mynode would be a URI that uniquely identifies the "myPrefix" namespace for all of the elements that use it within my XML document. This is NOT a URL because it is only used to identify, not to locate something per se.

    0 讨论(0)
  • 2020-11-22 02:57

    As per RFC 3986, URIs are comprised of the following pieces:

    scheme://authority/path?query
    

    The URI describes the protocol for accessing a resource (path) or application (query) on a server (authority).

    Enter image description here

    All the URLs are URIs, and all the URNs are URIs, but all the URIs are not URLs.

    Please refer for more details:

    Wikipedia

    0 讨论(0)
提交回复
热议问题