Friday, January 23, 2015

URLs vs URIs

Whenever I needed to work with a URL in my code I wasn't sure how to name the variable: `url` or `uri`? What term to use in documentation: URL or URI?

Some projects use term `URI` ( `Connection URI` in Flask,  `redirect_uri` in Facebook API). Other projects use term `URL` (like SqlAlchemy or `redis.from_url`).

I've been thinking about this occasionally in the last several years. Today, after reading again the Wikipedia article about URI I reached a conlusion.

A URN is a URI that identifies a resource by name in a particular namespace. A URN can be used to talk about a resource without implying its location or how to access it.
The International Standard Book Number (ISBN) system for uniquely identifying books provides a typical example of the use of URNs. ISBN 0-486-27557-4 cites unambiguously a specific edition of Shakespeare's play Romeo and Juliet. The URN for that edition would be urn:isbn:0-486-27557-4. To gain access to this object and read the book, its location is needed, for which a URL would have to be specified.

A URL is a URI that, in addition to identifying a web resource, specifies the means of acting upon or obtaining the representation, specifying both its primary access mechanism and network location. For example, the URL refers to a resource identified as /wiki/Main_Page whose representation, in the form of HTML and related code, is obtainable via HyperText Transfer Protocol (http) from a network host whose domain name is

In other words: the URN defines an item's identity, while the URL provides a method for finding it.

Or in my words, URN is like an identifier of a resource, and URL is identifier of one of its locations (but not resources global identity).

So, every URL is a URI, but not every URI is a URL.

So, if we talk about a database whose location we want to put into settings, we should use term URL, because it's a location (`postgresql://scott:tiger@localhost/mydatabase`)! Using an URN (something like `urn:database:6e8bc430-9c3a-11d9-9669-0800200c9a66`) would not work in these cases. So if we can't use URNs, we should not use term URI which includes URN.

No comments:

Post a Comment