spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

Relative URLs

Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

URLs, as I mentioned earlier, are used to locate anything, anywhere. Since their scope is so large, they often get very long. It is often the case that we can abbreviate URLs in the form of relative URLs.

All the URLs we've looked at until now were absolute URLs. They identified a resource independent of their context. The URL http://WebReference.com/html/ identifies HTML with Style whether it's typed into your browser, scribbled on a napkin in a bar for a friend or just remembered in your head. Relative URLs are a way to identify a resource relative to their context.

To give a rough analogy, if you're in your house with some friends and someone asks where your next-door neighbor Mark lives, you'll probably say "Oh, he's next door." You probably wouldn't say "On a planet called Earth, in the United Stated of America, in Acmetown, on 32 Foobar St." if this conversation is taking place in the living room of 31 Foobar St. For one thing, it's a waste of breath.

Relative URLs are something like that; they're the equivalent of saying "down the road, turn left, walk on for a few blocks, you can't miss it." They only make sense if they are given within a certain context. And this context is called a Base URL.

As we mentioned earlier, relative URLs can only be used with generic URLs like the ones described by the http scheme. The reason for this is that all generic URLs, like those defined by the http scheme, have a pathname. The Base URL of a resource is everything up to and including the last slash in its pathname. Here are some examples:

Table 1: Examples of Base URLs
Absolute URLBase URL
http://WebReference.com/http://WebReference.com/
http://WebReference.com/html/http://WebReference.com/html/
http://WebReference.com/html/about.htmlhttp://WebReference.com/html/
http://WebReference.com/foo/bar.html?bazhttp://WebReference.com/foo/

Now, say that in the document http://WebReference.com/html/about.html, I want to refer to the document http://WebReference.com/html/links.html. What I would do is use the relative URL links.html. This will be added to the base URL to form the URL that I want to match.

In most cases, figuring the absolute URL from a relative URL is just a matter of concatenating the base URL and the relative URL, as in the example above. There are, however, a few special cases:

A directory called .. (two periods) in a relative URL indicates the parent directory, essentially stripping off everything up to the previous slash in the the Base URI. Note that this only has meaning inside the pathname, so you cannot use this notation to go up higher than the root directory.

A directory called . (one period) refers to the current directory.

A relative URL that begins with / (a slash) always replaces the entire pathname of the base URL.

A relative URL that begins with // (two slashes) always replaces everything from the hostname onwards.

These notations are best illustrated by example, in the following table. The table shows relative URLs which are all assumed to have the base URL http://WebReference.com/html/, and the absolute URLs that they translate into:

Table 2: Resolving relative URLs relative to http://WebReference.com/html/
Relative URIAbsolute URI
about.htmlhttp://WebReference.com/html/about.html
tutorial1/http://WebReference.com/html/tutorial1/
tutorial1/2.htmlhttp://WebReference.com/html/tutorial1/2.html
/http://WebReference.com/
//www.internet.com/http://www.internet.com/
/experts/http://WebReference.com/experts/
../http://WebReference.com/
../experts/http://WebReference.com/experts/
../../../http://WebReference.com/
./http://WebReference.com/html/
./about.htmlhttp://WebReference.com/html/about.html

Also note that a relative URL that is empty (consisting of no characters) points to the resource in which it exists.

Previously, I mentioned that relative URLs save time typing. Indeed they do, but that is not the reason for their use. There are other ways to save time if you're that lazy, such as editors with shortcut features. But relative URLs are much more important than that: they point to a resource relative to their context. This is very important, because URIs may not always be used in the same context. But we'll explain that later on after we've explained hyperlinks.

But before we do that, we'll take a look at a few other commonly used URL schemes.

Front Page12345678

http://www.internet.com

Produced by Stephanos Piperoglou


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Software Engineering for Ajax · Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-FiPlanet Gift Guide, Class of 2008 · Patriot Updates their DDR2 4GB PC2-8500 Line to "Revision 2" · Review: Samsung SCX-4500 Multifunction Printer


All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/html/tutorial2/3.html
Created: June 11, 1998
Revised: June 11, 1998