Absolute and relative URLs
The URI general syntax allows a resource to be specified as an absoluteor a relative URL. Absolute URLs identify the protocol http://, the host, and the path of the resource, and can be used alone to locate a resource. Here's an example absolute URL:
http://example.com/documents/glossary.html
Relative URLs don't contain all the components and are always considered with respect to a base URL. A relative URL is resolved to an absolute URL, with respect to the base URL. Typically, a relative URL contains the path components of a resource and allows related sets of resources to reference each other in a relative way. This allows path hierarchies to be readily changed without the need to change every URL embedded in a set of documents.
A web browser has two ways to set base URLs when resolving relative URLs. The first method allows a base URL to be encoded into the HTML using the <base> element. The second method sets the base URL to that of the current document; this is the default. For example, the following HTML document contains three relative URLs embedded into <a> elements:
<html>
<body>
<h2>My Home Page</h2>
<p>Read my <a href="cv.html">Curriculum Vitae</a>
<p>Read my
<a href="work/emp.html">employment history</a>
<p>Visit
<a href="/admin/fred.html">Fred's home page</a>
</body>
</html>
Consider what happens if the example is requested with the following URL:
http://example.com/development/dave/home.html
The three relative URLs are resolved to the following absolute URLs by the browser:
http://example.com/development/dave/cv.html http://example.com/development/dave/work/emp.html http://example.com/admin/fred.html
Table B-1 shows several relative URLs and how they are resolved to the corresponding absolute URLs given the base URL http://example.com/a/b/c.html?foo=bar.
Table B-1. Example relative URLs resolved to absolute URLs
| Relative URL | Absolute URL with respect to http://example.com/a/b/c.html?foo=bar |
|---|---|
| d.html | http://example.com/a/b/d.html |
| e/d.html | http://example.com/a/b/e/d.html |
| /d.html | http://example.com/d.html |
| ../d.html | http://example.com/a/d.html |
| #xyz | http://example.com/a/b/c.html?foo=bar#xyz |
| ./ | http://example.com/a/b/ |
| ../ | http://example.com/a/ |