Relative URLs

When a web browser reads an HTML document, it has a great deal of information about the document. This includes the protocol used to retrieve the document, the name of the host where the document lives, and the path to that document on the host. Most of this is likely to be the same for many of the URLs in that document. Relative URLs inherit the protocol, hostname, and path of their parent document rather than respecifying it in each <A HREF> tag. Thus if any piece of the URL is missing, it is assumed to be the same as that of the document in which the URL is found. Such a URL is called a relative URL. In contrast, a completely specified URL is called an absolute URL. For example, suppose while browsing http://metalab.unc.edu/javafaq/books.html you click on this hyperlink:

<a href="javafaq.html">the FAQ</A>
Your browser cuts books.html off the end of

http://metalab.unc.edu/javafaq/books.html

to get

http://metalab.unc.edu/javafaq/

Then it attaches javafaq.html onto the end of http://metalab.unc.edu/javafaq/ to get

http://metalab.unc.edu/javafaq/javafaq.html

If the relative link begins with a /, then it is relative to the document root instead of relative to the current file. Thus if while browsing http://metalab.unc.edu/javafaq/books.html you clicked on this hyperlink:

<a href="/boutell/faq/www_faq.html">
Your browser would throw away /javafaq/javafaq.html and attach /boutell/faq/www_faq.html to the end of http://metalab.unc.edu to get http://metalab.unc.edu/boutell/faq/www_faq.html.

Relative URLs have a number of advantages. First and least they save a little typing. More importantly relative URLs allow entire trees of HTML documents to be moved or copied from one site to another without breaking all the internal links.


Previous | Next | Top
Last Modified June 17, 1998
Copyright 1997-8 Elliotte Rusty Harold
elharo@metalab.unc.edu