Another benefit is that since relative links don't refer to the server or directory, it is easier to move related pages from one location to another without having to change the links. This allows a person to create a set of linked pages and images on their local hard disk, test the pages locally, and move them up to the actual web server without having to change anything.
[ host ] [ path ] [ file ]
For all the examples here, we'll use a webpage with the URL:
http://www.med.und.nodak.edu/users/staff/barryp/home.htm
which breaks down as:
host: [http://www.med.und.nodak.edu]
path: [/users/staff/barryp/]
file: [home.htm]
Note that:
http: ftp: gopher: mailto:
and so on.
/. This
slash is important later on in deciding whether a path is relative or not.
http://www.med.und.nodak.edu/misc/undsom.gifthat breaks down into:
host: [http://www.med.und.nodak.edu]
path: [/misc/]
file: [undsom.gif]
you could leave out the host part of the URL, since it's exactly the same as the webpage's host part - but you'd
have to keep the path since it's different.
So you could embed the image with a tag like:
<img src="/misc/undsom.gif">
Note that this relative URL begins with a forward slash, so a web client will know that the relative URL includes the path, and only the host is missing.
http://www.med.und.nodak.edu/users/staff/barryp/extra.htmbreaks down into:
host: [http://www.med.und.nodak.edu]
path: [/users/staff/barryp/]
file: [extra.htm]
and in this case, both the host and path parts match the
base document's, so they can be left out and the simple form of the link can be:
<a href="extra.htm">
In this case, since the relative URL didn't begin with a forward slash, a web client knows to use
the base document's path.
http://www.med.und.nodak.edu/users/staff/barryp/images/mypic.gif
Breaks down into:
host: [http://www.med.und.nodak.edu]
path: [/users/staff/barryp/images/]
file: [mypic.gif]
In this case, the host part of the URL is exactly the same as the webpage's, so that can be left out.
The path part is the same as the webpage's, but with images/ added to the end.
In this case, the part of the path that matches the
webpage's path can be left off, leaving the relative URL:
<img src="images/mypic.gif">
/users/staff/barryp/] in front of the relative URL, giving:
/users/staff/barryp/images/mypic.gif
as the combined path/filename of the URL. Next, since the host information is missing, the client
will throw in the base document's host info [http://www.med.und.nodak.edu] to come up with the full url:
http://www.med.und.nodak.edu/users/staff/barryp/images/mypic.gif
all from our initial relative url of images/mypic.gif.
| Barry Pederson <barryp@medicine.nodak.edu>
Revised 7/26/2001 |