The Difference Between URI and URL
by Nicklas EnvallURI and URL sound similar and looks similar and are even used interchangeably. So, what is the difference between a URL and URI? Well, that question causes confusion because a URL is a URI, but a URI is not always a URL. URI stands for Uniform Resource Identifier, which is a string that is used to identify a resource. URL stands for “Uniform Resource Locator” and is a specific type of URI and is used to locate a resource on the Internet.
Now when we know that a URL always is a URI, a better question to ask rather than what separates a URL and URI, would be, "what is a URI?".
1. What is a URI?
A Uniform Resource Identifier (URI) can be a name, locator, or both. So, a URI may state the name (URN) or the location of a resource (URL). A URN and URL is always a URI, but not the other way around. We define a resource's identity with a URN and locate it with a URL.
But what does identifying or locating a resource even mean? The answer is that when we talk about resources, we talk about digital, physical or abstract things that we can identify.
- URL: Is a subset of a URI, which gives means to locate a resource.
- URN: Is a subset of a URI, which is a name that gives a resource an identity.
URL
URL is an abbreviation for “Uniform Resource Locator” and is a specific type of URI. An URL identifies resources and provides the network location for the respective resource. This means that a client may use an URL to retrieve a resource.
URN
URN is an abbreviation for Uniform Resource Name. A URN is a URI that is assigned under the "urn" URI scheme and a particular URN namespace, with the intent that the URN will be a persistent, location-independent resource identifier.
2. URI Syntax
The URI syntax consists of the following components in a hierarchical sequence: scheme, authority, path, query, and fragment.
scheme://authority/path?query#fragment
- Scheme: is a name that refers to a specification for assigning identifiers within that scheme. A scheme is, for example, http, data, ftp, mailto, etc.
- Authority: is optional and always introduced with a double slash, ("//"). The authority component ends either with a slash ("/"), question mark ("?"), hashtag ("#") or simply nothing. It has three subcomponents, userinfo, host, and port.
- Path: identifies a resource within a scheme, and if used, authority. A path is always defined, but the path can be empty. If the URI has an authority component then the path must always either begin with a "/" or be empty.
- Query: identifies a resource just like the path component but does this additionally.
- Fragment: identifies a specific part of the resource, which is optional.
All URI starts with a scheme, which means that how a URI looks like vary depending on which scheme you use. This is because schemes may have their own syntax like scheme:scheme-specific-part
. Down below we see four different URIs:
ftp://ftp.is.co.za/rfc/rfc1808.txt http://www.example.com/article?id=1#middle mailto:[email protected] tel:+1-816-555-1212
Let's look at the following URI http://www.example.com/article?id=1#middle
. http
is the scheme, www.example.com
is the authority, /article
is the path, ?id=1
is the query part, and lastly, #middle
is the fragment part.
Bonus tip for web developers
Knowing the difference between URI and URL as a web developer is a great idea. If you are a web developer, I recommend that the next time you write some HTML-code, you try different URIs out by using anchor tags. For example like this:
<a href="http://www.example.com">Go to example.com</a> <a href="mailto:[email protected]">Email John Smith</a> <a href="tel:+numberhere">call +numberhere</a>