Absolute and Relative Url
Absolute and Relative Url
In addition to several other meanings, the word absolute, in English, means "not
dependent on anything else". It also means "free from doubt".
An Absolute URL is, thus, something that is independent or free from any
relationship. When you use an absolute URL, you point directly to a file. Hence,
an absolute URL specifies the exact location of a file/directory on the internet. It
also follows that each absolute URL is unique, which means that if two URLs are
identical, they point to the same file.
For example:
http://www.webdevelopersnotes.com/images/email.gif specifies an image file
email.gif located in the images directory, under www.webdevelopersnotes.com
domain name.
Similarly, the absolute URL of the document you are viewing is
http://www.webdevelopersnotes.com/design/ relative_and_absolute_urls.php3 which
is a page in the directory called design on this web site.
Relative URL
A relative URL points to a file/directory in relation to the present file/directory.
<img src="http://www.webdevelopersnotes.com/images/email.gif"
width="..." height="..." />
The absolute URL is straight forward but in the relative URL you'll notice that I
have refered to the image with ../images/email.gif. In order to understand the
relative URL, let me tell you about the directory structure of this web site.
This web site has several sections and the files and web pages for each section
have been segregated into different directories. This helps me to keep things
organized and uncluttered on the web site. Under the document or server root
directory (the main directory of the web site), I have a directory called images
which stores all common images used on the pages of this web site. The image
email.gif resides in this directory. I have another directory called design which is at
the same level as images i.e. it is also in the document root directory. This design
directory contains the files and web pages for the "Web Page Design" section of
this web site. Diagramatically, the scenario can be represented as:
Finally, I have not known of any server performance benefits with the use of a
particular URL type.
What is the difference between an absolute and a
relative URL?
An absolute URL typically takes the following form:
protocol://hostname/other_information
If the file you want to link to is in a subdirectory of the directory the referring page
is in, you need to enter only the directory information and the name of the file. So
if foobar.html were in the foobar subdirectory of your www directory, you
could refer to it from your home page by using:
If the file you want to link to is in a higher directory than the referring page, use
.. , which means to go up a directory. For example, to link from foobar.html
to home.html, which is in the directory above, you would use: