What are Relative and Absolute urls (paths)

Web programming topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

What are Relative and Absolute urls (paths)

Post by Saman » Wed Apr 21, 2010 6:09 am

Relative URL
The most useful URL path is the relative path! The relative URL points to a file or directory in relation to the present file or directory (folder). Relative URL's help in web site maintenance. It is easy to move a file from one directory (folder) to another, or a web site from one domain name to another. And you don't have to worry about updating the link(s) or the src (img) path(s).

Keep It Simple Stupid (KISS): If you are loading a file / image / etc. that is in the same directory as the source file, just use the file name. If the html file is in the top directory and need to link to an image in that directory, the relative URL would be <img src="top.gif" />. This is the very basic - simple example of a relative path because the file and directory are both in same place. In other words, no path info is required if the files are in the same directory. (very convenient and easy to manage).

For next level: To access a sub-directory below the top directory - do NOT use the preceding slashes. For example, if you Home page is the index.html and the logo (robot.lk.gif) image is in the images directory, use:
<img src="images/robot.lk.gif" /> (note the omission of the slash :) )

For more details - Here is example on a web server (where the web root is public_html)

Root (public_html)
.......index.html
.......top.gif

....images
......robot.lk.gif

....help
......path
........index.php (this page)

For this page, the current page is the index.php file inside the path directory - inside the help directory. Therefore, the relative path to this page is
/help/path/index.php

Then to load the robot.lk.gif image (top left of this page), the relative path to the image is
../../images/robot.lk.gif
Which means the robot.lk.gif image is in a directory two levels up from this index.php file - then down into the 'images' directory.

The two dots .. instruct the server to move up one directory. Therefore two sets of ../../ moves up two levels to the root directory (public_html) - then opens the images directory and loads the robot.lk.gif file.

Of course, if the image had been in the help directory the relative path would be
../help/imagename.jpg

Relative path is really easy to understand just by reviewing the basic definition:
A relative URL points to a file or directory in relation to the present file or directory.

Absolute URL
An absolute URL is the URL most people already understand. It is the complete path including the domain - file name. Example: https://robot.lk/images/logo.gif specifies an image file (logo.gif) located in the images directory, for the https://robot.lk domain. This type of URL is what your must use when you want to link (or load) a file that is on another server.

Another example is the absolute URL of this page (which is also the Address / Location of the file) = https://robot.lk/help/path/index.php

Absolute vs. Relative Server Paths
Absolute Paths
Paths are used ON THE SERVER. Usually inside a script. The absolute path is the "full path". It is the path that contains the top directory (usually home) - the username - the root directory (usually public_html) - the sub directory (or file name) For example
/home/username/public_html/cgi-bin/test.cgi
This is the absolute path to the text.cgi file in the cgi-bin directory

BTW: /home/username/ would put you in the root of your account. An section that is NOT accessible via WWW (browser) - this is where mail files, password files, etc (other protected files) are stored on the server. You seldom need this path since you are working with web pages and need path info to the public_html (www) directory ( your root directory FOR the web)

Example of other useful path information shows the path to shared files on the server. For example the path to perl on the server:
/usr/bin/perl
Path info of this type are supplied by the host when you need it for scripts. In most cases, the cPanel account displays this info for the client. Along with path to 'date' 'sendmail' etc that are required by many scripts.

Relative Path
Relative Path may be a misnomer. It is actually like the relative url i.e. it is normally 'relative' to the public_html directory (which is the root directory for the web site / not the root of the server). However, it could be relative to the upper directories (home). But in actually use, the relative path is seldom used - even in scripts. And if it is required, the script writer may NOT use the name - relative path. It may be referred to as a non-absolute path, a direct path, a path to a file, etc. Bottom line - is that scripts that refer to the relative path - really mean the path to files below the www root (usually public_html). So it is usually the same as the relative URL. And in actual practice it's not 'usually called' a relative path. It's more likely to be referenced as the 'path to files'.
Post Reply

Return to “Web programming”