Relative Vs. Absolute Paths

From Acenet Knowledgebase
Jump to: navigation, search

This article will show the difference between relative and absolute paths.

Absolute Path

An absolute path is when you specify the full path that is taken when reaching a specific destination. Here are some examples of absolute paths and where they may be used:

Linux Absolute

nano /home/testuser/public_html/testdir/example.txt

For the above example, the absolute path includes every directory that needs to be accessed in order to view the text file example.txt.

Absolute Internet URL

http://testdomain.com/examplepage.txt

In this example, the absolute path includes the domain you are viewing, and the specific web page on that domain.


Relative Paths

A relative path is when you specify the rest of the path as to where you currently are. Here are some examples of relative paths and where they may be used:

Linux Relative

nano testdir/example.txt

In the above example, I am using a relative path to view a text file example.txt. I have already navigated to, and am currently located in:

home/testuser/public_html/

So, since I am already located within home/testuser/public_html/, I can simply use the relative path to view the example.txt file.

Relative Internet URL

Relative paths do not work when visiting a webpage, but are highly recommended when displaying images or configuring scripts. Here is an example of a relative path used for uploading an image:

<img src="testdir/example.jpg" />

The relative path in the above example is:

testdir/example.jpg

Relative paths are highly recommended over absolute paths when displaying images or configuring scripts, here are some reasons as to why:

Changing Your Domain

If you were to change your domain name, the relative path would still work. If you were to use an absolute path and changed your domain name, then you would need to update your file that is referencing your image to use the new domain name.

SSL certificates

If you were to install a SSL on your domain, while using an absolute path of "http" specified in the file that is referencing your image, when you view your image you would receive a warning of "This is not trusted". The reason being is because SSLs use "https", and you specified in the file that is referencing your image to use "http". In order to make the "This is not trusted" warning disappear, you will need to update the file that is referencing your image to use "https".

Now say you are using a relative path and you have a SSL installed. When you visit your image, you will not receive a warning of "This is not trusted" because the file that is referencing your image does not care if you use "http", or "https". This is because when using a relative path, you are telling the file that is referencing your image to use the type of connection you specify in the URL.