We all use many URLs on a daily basis. Sometimes we type them in, while sometimes, we are just redirected to one URL by another. It is interesting to know that most of us think http://google.com or http://facebook.com are just URLs, but only a few know that we can also refer to them as the URI.
Many who first hear about URI vs URL can get quite confused as they try to tell the difference between both. So what exactly is the difference between the two, and how can you tell them apart?
What’s a URI?
URI stands for Uniform Resource Identifier. It could be a name, locator or both. URI can contain URL and URN. However, when the URI is just a name, that means it is not a URL.
URI contains scheme, authority, path, query, and fragment in that same order. Some of the most common URI schemes that we use are HTTP, https, ftp, ldap, mailto, telnet, gopher, news, etc…
The generic URI syntax follows IEFTs RFC 3986.
The syntax is –
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
Before I throw some examples for you, I thought it would be nice to know why it is called ‘Uniform’. Why not just Resource Identifier?
Uniformity not only allows for different resource identifiers to be used in the same context, but it also allows for a uniform semantic representation of common conventions across the resource identifiers. That also means it is easier to have new resource IDs introduced without interfering with existing ones.
Now to the examples –
https://hackr.io/blog - this is the typical URI that we know of and call it a URL.
How about this one?
Tel:+1-816-345-1212
The above is a URI but not a URL. It identifies a resource. A resource can be anything. It need not be a web service always. It can be an image, a document, or a collection of several resources.
Examples of URIs
- mailto:support@examplewebsite.com
- Tel:+1-816-345-1212
- ldap://[2007:db5::2]/c=GB?objectClass?one
- telnet://192.0.2.16:80/
Important Reasons to Use URI
If you’re wondering why you should use URIs, here are a few reasons that can help answer your question:
- The URI as we know it is vital to our current semantic web because it can help to prevent ambiguity
- URI searches a resource or file’s name and location using a uniform format
- URI can provide methods through which other systems can access resources over the internet or the network; thus, Peer-to-Peer or P2P file-sharing software such as BitTorrent and web browsers use URI to find files for download
- A URI allows you to define new file types without affecting any of your old files
What’s a URL?
URL stands for Uniform Resource Locator.
Always remember – URI can contain a URL, butthe URL specifies only the address of a resource. As the name says, it is a ‘Locator’. URI is thus broader than the URL.
URL contains the following information:
- The protocol that is used for accessing the resource – HTTP: https: ftp: etc…
- Location of the server using IP address or domain name – for example, hackr.io is the domain name. http://198.152.1. 1 – here a resource is located using its IP address (198.152.1.1)
- The port number on the server. For example, http://localhost:8080, where 8080 is the port. It is not mandatory to mention the port.
- The exact location in the directory structure of the server. For example – http://hackr.io/blog is the exact location if a user wants to go to the blog section of the website.
- An optional fragment identifier. For instance, https://www.google.com/search?ei=ARDsXO-1EL7az7sPvbWigAo&q=URL where q=URL is the query string user typed.
Examples of URLs
Important Reasons to Use URLs
If you’d like to know why you should use URLs, the reasons below can help:
- The info that is written in a URL allows you to switch from website to website (or web page to web page) with a single click
- URLs tell you specifically how to access a resource
- URL follows a set format that is clear and easy to remember
- URL makes it easier to navigate between pages and directories
- Clicking any hyperlinked text or typing a URL into any browser will make your web browser send requests to web servers to download a file(s)
- A URL can be used as the address of any unique resource found on the web. It can be used for websites and pages, files, images, videos, web apps, and so on
- A URL, also known as a website’s domain, is generally one of the most vital parts of a website. Using simple words or phrases ending with a TLD or top-level domain such as .com, .net, .org, and .edu can make it easier for people to find a website, increasing traffic as a result
What’s a URN?
A URN, or a Uniform Resource Name, identifies a resource (by name) on the internet. Where a URL uses network addresses such as the domain, directory, and file name, a URN uses regular words that are independent of protocol and location.
A URN is persistent (meaning it will never change) and provides a higher level of abstraction. It requires a resolution service, much like a DNS system, to convert a name into a real address.
In the debate between URI vs URL, the URN kind of falls to the wayside. You’ll see people using the term URI or URL, but you’ll hardly find people using URNs in day-to-day life.
All URLs Are URIs, But Not All URIs are URLs
The URI, in fact, is a combination of a URL and a URN. We can thus safely conclude that all URLs are URIs. However, the opposite is not true.
The main reason that all URLs are URIs but not the other way around is that a URI can be a resource’s name or location, while the URL is only the location.
A quick real-life example to understand this would be:
Your name, John, is the URN. Your address, say #34 Canter Street, is the URL. You can be identified as a unique person with your name or your address. That unique identity is the URI.
While your name can be your unique identifier (URI), it can’t be a URL because your name doesn’t help you find your location. In other words, URI (which are URN) are not URLs.
How to Identify Whether Something is URI vs URL
If you want to know if something is a URI or URL, you should always mark it as a URI because all URLs are URIs.
However, if you’re still scratching your head and wondering, “what is the difference between URL and URI?,” you can see their key differences below:
- The URI contains both the URL and URN, which identify a resource’s location and name respectively. The URI can also bejust a URL or just a URN, allowing it to still identify only a resource’s name or location.
- The URL falls within URI as a subset. It identifies only the location of a resource.
- You can use a URI to find resources in XML, HTML, and other files.
- You can only use a URL to locate and access a specific website or page.
- All URLs are also URIs. Not all URIs are URLs.
Head-to-Head Comparison: URI vs URL
Let’s do some side-by-side comparisons so that whatever we have discussed so far is reinforced and you will not get confused about using URI and URL incorrectly ever again!
URI |
URL |
Identifies a resource by name, location, or both |
Locator of a resource |
Superset of URL |
Type of URI |
Syntax –
where
|
Syntax –
|
The scheme can be anything – protocol, name or specification, etc… |
The scheme is always a protocol like HTTP, https, ftp, LDAP etc… |
The main purpose of URI is to identify a resource and distinguish it from other resources using either location or name |
The main purpose is to get the address or location of a resource |
Example – contact: +1 883-345-1111, urn:isbn:1234567890 |
Example – https://hackr.io/blog |
Used in HTML, XML and tag library files like XSLT and JSTL to identify resources and binaries. |
URL is used to find only web pages |
Java API Differences
There are some differences in the way we instantiate URI and URL objects in Java. URI has a specific constructor to give out all parts of the syntax separately, whereas the URL doesn’t. In this case, the URL-URI difference is that URL has methods to open a remote connection to obtain the contents of the pointed resource.
URI vs URL: Which One Should You Use?
When people get past asking “what is a URI vs URL?” and start debating which one is better than the other, it’s not uncommon for people to start correcting each other and insisting that their opinion is correct.
But which one is actually correct? Should you use URI or URL?
The answer is — well, it depends. It’s all a matter of the topic of discussion. If people are talking about your average everyday website (with a domain) like hackr.io or google.com, you’re probably better off using a URL. The reason behind this is that when someone mentions a domain, it implies the HTTP protocol. Thus, it is a URL.
Specificity is best in most cases. URL is used because it is more specific (even though you can still call hackr.io or google.com as URI).
For example, if you are having a conversation with someone and they ask where they can start learning about programming, you can tell them that they can try a resource website. Alternatively, you can specifically say the URL Hackr.io, which is a programming resource website. Giving them the URL makes it easier for them to locate what they are looking for.
In the end, you can use URI because it’s technically correct, but if you want to be more specific (which you likely want to be in most cases), you’re probably better off using URL.
Conclusion
While more people commonly know and use the term URL every day, you can pretty much use URI in general contexts as well since it is a broader term.
URL and URN are both specific and limited. The difference between URI and URL is very subtle and only occurs when URI specifies only the name. When it comes to location, URI is the same as the URL.
I hope that this article exploring URI vs URL has helped you better understand both as well as their similarities and subtle differences. Hopefully, you now know when to use URL or URI!
Getting ready to find work? Prep yourself for your interviews with these sample web services interview questions.
People are also reading:
- What is Procedural Programming?
- What is Functional Programming?
- Top Web API Interview Questions
- Top Programming Terms for beginners
- How to Learn Programming?
- Which Programming Language is best for getting Jobs?
- Difference between Structure vs Union
- CSS Cheat Sheet
- PHP Cheat Sheet
- SQL Cheat Sheet
Frequently Asked Questions
1. Is URI the same as URL?
All URLs are URIs. This is because a URI is comprised of URN or URL — or both. However, not all URI are URL.
2. What is the difference between URL and URI with example?
A URL is a locator of a resource. On the other hand, a URI can identify a resource by a name, a location, or both.
- Example URI: ldap://[2007:db5::2]/c=GB?objectClass?one
- Example URL: http://google.com
3. Is a domain name a URI?
It can be. A domain name (for example, google.com) is part of a URL, which helps to locate a website on the internet. For example, simply typing google.com into your browser will take you to the Google website, effectively locating the resource.
4. Is URI an API?
Not necessarily. There may be some confusion between URI and REST API. If you’d like to know more, you can see this thread on StackOverflow.
5. How do I change my URL to URI?
If you’d like to learn more about changing your URL to URI, this tutorial can show you how to do it in Java.