Difference Between an Absolute and a Relative URL

April 2023 · 3 minute read

an Absolute vs a Relative URL

Uniform Resource Locator (URL) is an address that specifies where a particular document or a resource is located on the World Wide Web (WWW). Best example of a URL is the address of a web page on the WWW such as http://www.cnn.com/. Absolute URL, also called an absolute link is a complete internet address that takes a user to the exact directory or file of a website. A Relative URL or a partial internet address, points to a directory or a file relative to the current directory or a file.

What is Absolute URL?

Absolute URL, which provides a complete address of a web page or a resource on the WWW, generally has the format given bellow.

protocol://hostname/other_details

Usually, the Hyper Text Transfer Protocol (http://) is used as the protocol section. But the protocol also could be ftp://, gopher://, or file://. The hostname is the name of the computer that the resource is residing. For example, the hostname of the CNN’s central web server is www.cnn.com. The other_details section includes information about directory and the file name. The exact meaning of the other_details section depends on both the protocol and host. The resource that is pointed by the absolute URL is normally resided on a file, but it can also be generated on the fly.

What is Relative URL?

As mentioned earlier, a relative URL points to a resource relative to the current directory or file. A relative URL can take several different forms. When referring to a file that resides in the same directory as the currently referred page, the relative URL can be simple as the name of the file itself. As an example, if you need to create a link in your home page to a file called my_name.html, which resides in the same directory as your home page, you can simply use the file name as follows:

<a href=”my_name.html”>My name</a>

If the file you need to link is within a sub directory of the referring page’s directory, you need to include the subdirectory name and the file name in the relative URL. For example if we are trying to link a file my_parents.html that is within a directory called parents, which actually resides inside the directory that contains your home page, the relative URL will look like the following.

<a href=”parents/my_parents.html”>My Parents</a>

Additionally, if you want to refer to a resource that resides on a directory that is in a higher level in the directory structure than the directory that contains the referring page, you can use two consecutive dots. For example, if you want to refer to a file called home.html that in a directory above your home page, you can use a relative URL as follows.

<a href=”../home.html”>Home</a>

Difference between Absolute URL and Relative URL

The main difference between an absolute URL and a relative URL is that, an absolute URL is a complete address that points to a file or a resource, while a relative URL points to a file relative to the current directory or file. Absolute URL contains more information than a relative URL, but using relative URLs are much easier since they are shorter and more portable. But relative URLs can only be used to refer to links that reside on the same server as the page that refers them.

ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau26tzWaYm6ufocK1sYyapZ1lpqh6onnRnqOarJmrsm7B0aVm