DomainsCategory

URI vs URL: the main differences between URL & URI

9 min read
Adem Asha

URI vs URL. These two terms are often used interchangeably, but they have distinct differences that are important to understand.

In this article, we will delve into the subtle differences between URLs and URIs and discuss how they are used in the context of the internet.

Types of URIs

What is URI and what does URI stand for?

A URI, and acronym of Uniform Resource Identifier, is a sequence of characters that identify a name or a unique resource on the Internet. URIs can be broken down into two main categories: URLs (Uniform Resource Locators) and URNs (Uniform Resource Names).

A URI contains scheme, authority, path, query, and a fragment. Some most common URI schemes are HTTP (Hypertext transfer protocol), HTTPs, ftp, Idap, telnet, etc.

URL (Uniform Resource Locator) is often defined as a string of characters that is directed to an address. It is a very commonly used way to locate resources on the web. It provides a way to retrieve the presentation of the physical location by describing its network location or primary access mechanism.

A URN, on the other hand, is a type of URI that identifies a resource by its name rather than its location. URNs are used to provide a persistent and location-independent way to identify resources. For example, a URN might be used to identify a specific book in a library catalog, regardless of where the book is physically located.

What is URL & what does URL stand for?

As mentioned above, a URL, or Uniform Resource Locator, is a string of characters that specifies the location of a resource on the internet. URLs are used to access web pages and other resources on the world wide web, such as images and videos.

A URL typically consists of several parts, including the protocol (such as "http" or "https"), the domain name (such as "example.com"), and the path to the specific resource (such as "/index.html"). For example, the URL "https://www.example.com/index.html" specifies the location of the "index.html" page on the "example" website using the "https" protocol.

URLs are a specific type of URI (Uniform Resource Identifier) and are used to locate resources on the internet. They provide a standard way to identify and access resources, regardless of where they are located or how they are being accessed. URLs are a key component of the internet and are used by web browsers to retrieve and display web pages and other web resources.

This tutorial tells what you need to know about URLs:

Main difference between URL and URI

The main difference between a URL and a URI is that a URL specifies the location of a resource on the internet, while a URI can be used to identify any type of resource, not just those on the internet.

URI is usually used in XML, tag library files, and other files, such as JSTL and XSTL.

URL syntax

A URL has a specific syntax, which includes the following parts:

  • Protocol: The protocol specifies how the resource should be accessed and is indicated at the beginning of the URL by a scheme name followed by a colon (e.g., "http:", "https:", "ftp:", etc.).
  • Domain name: The domain name identifies the web server on which the resource is located and is usually the second part of the URL, following the protocol.
  • Path: The path specifies the location of the resource on the server and is indicated after the domain name. The path can include subdirectories and the name of the resource (e.g., "/images/logo.png"). NSS – this is a namespace-specific string that identifies the internet resource. It may contain ASCII codes, digits, punctuation marks, and special characters.

URL examples

Here are some examples of URLs:

File transfer protocol (FTP) – used for a file transfer between different devices via a network. e.g. from your phone to your computer as long they are connected to the same WiFi network.

URI syntax

A URI, on the other hand, can be used to identify any type of resource, not just those on the internet. URIs can be further divided into two categories: URLs and URNs.

A URL is a type of URI that specifies the location of a resource on the internet and includes a protocol at the beginning. A URN is a type of URI that identifies a resource by its name rather than its location.

URI examples

Here are some examples of URIs:

  • "urn:isbn:978-3-16-148410-0" (a URN that identifies a book by its ISBN)
  • "mailto:info@example.com" (a URI that specifies an email address)
  • "tel:+1-212-555-1212" (a URI that specifies a phone number)

URI vs URL vs URN

A URI, or Uniform Resource Identifier, is a string of characters that identify a name or a resource. URIs can be used to identify any type of resource, such as a web page, an image, a video, or a specific piece of information.

A URL, or Uniform Resource Locator, is a specific type of URI that specifies the location of a resource on the internet. URLs are used to access web pages and other resources on the internet, and typically include a protocol (such as "http" or "https"), a domain name, and a path to the specific resource.

A URN, or Uniform Resource Name, is another type of URI that is used to identify a resource by its name, rather than its location. URNs are used to provide a persistent and location-independent way to identify resources.

Between a URI or a URL and their usage is essential for every webmaster. For example, building a REST API using a predictable and hierarchical structure of a URI or a URL can improve the REST API performance.

A query is an optional component that contains a query string of non-hierarchical data. It is often a string of key=value pairs. This component is preceded by a question mark.

In summary, a URI is a generic term that refers to a string of characters that identify a resource, while a URL is a specific type of URI that specifies the location of a resource on the internet. A URN is another type of URI that is used to identify a resource by its name.

What is a URN?

As mentioned above. a URN is a type of URI that identifies a resource by its name rather than its location. URNs are used to provide a persistent and location-independent way to identify resources.

For example, a URN might be used to identify a specific book in a library catalog, regardless of where the book is physically located.

URI vs URL frequently asked questions (FAQs):

Some answers to your frequently asked questions about URI vs URL;

Should I use URL or URI?

It depends on what you are trying to do. If you are trying to access a resource on the internet, such as a web page or an image, you should use a URL. A URL is a specific type of URI that specifies the location of a resource on the internet and includes a protocol at the beginning.

If you are trying to identify a resource by its name, rather than its location, you should use a URN (Uniform Resource Name). URNs are used to provide a persistent and location-independent way to identify resources.

If you are not sure which type of URI to use, you can use a generic URI. A URI is a generic term that refers to a string of characters that identify a resource. It can be used to identify any type of resource, not just those on the internet.

How to convert URL to URI?

To convert a URL to a URI, you can simply use the URI class in the java.net package in Java. Here is an example of how to do this:

import java.net.URI;

import java.net.URISyntaxException;

public class Main {

public static void main(String[] args) throws URISyntaxException {

String urlString = "https://www.example.com/index.html";

URI uri = new URI(urlString);

System.out.println(uri);

}

}

This will output a URI object with the following string representation: "https://www.example.com/index.html".

You can also use the URL class in the java.net package to convert a URL to a URI. Here is an example of how to do this:

import java.net.URI;

import java.net.URL;

public class Main {

public static void main(String[] args) throws Exception {

String urlString = "https://www.example.com/index.html";

URL url = new URL(urlString);

URI uri = url.toURI();

System.out.println(uri);

}

}

This will also output a URI object with the string representation "https://www.example.com/index.html".

Note that both of these examples will throw a URISyntaxException if the URL string is not a valid URL. You should catch this exception or declare that your method throws it if you are using these examples in your code.

Is a URL also a URI?

Yes, a URL is a specific type of URI (Uniform Resource Identifier). A URI is a string of characters that identify a name or a resource, while a URL is a subset of URI that specifies the location of a resource on the internet.

The key difference is: all URLs are URIs, but not all URIs are URLs. URIs are a superset of URLs that can be used to identify any type of resource, not just those on the internet, while URLs are specifically used to locate resources on the internet.

For example, the following are valid URLs:

Both of these URLs are also URIs because they identify a resource (a web page or a file, respectively). However, not all URIs are URLs because they do not all specify the location of a resource on the internet.

For example, the following are valid URIs but not URLs:

  • "urn:isbn:978-3-16-148410-0" (a URN that identifies a book by its ISBN)
  • "mailto:info@example.com" (a URI that specifies an email address)

These URIs do not specify the location of a resource on the internet and are not used to access web pages or other resources.

In short, URIs can be either a URN or a URL. This fuzzy definition is thanks to RFC 3986 produced by W3C and IETF.

Products Used

DomainsLearn more