6.2. Absolute versus Relative Paths

Two styles of paths can be used in URLs: absolute and relative. Absolute paths contain all the relevant information to find the resource indicated by the URL. Relative paths contain information relative to the current document. For example, suppose that the user agent had loaded a document from the following URL:

http://www.example.com/products/gizmo.html

Suppose the document has a link to another document, doodad.html, which resides on the same server, in the same directory. Both of the following URLs can be used to reference the other document:

http://www.example.com/products/doodad.html
doodad.html
./doodad.html
http:doodad.html

The first URL uses an absolute path to the document—everything from the protocol, server name, and path to the document are specified. The other three URLs are relative—they contain only enough information for the document to be found relative to the location of the current document.

If you don't specify the protocol in a URL, the user agent will attempt to use its default protocol to request the document.

Note that relative paths can be used only with documents on the same Web server because documents on other servers require substantially more information to guide the user agent to them. Relative paths are best used on sections of Web sites where the documents in the section never change relationships to one another. In most cases, absolute paths should be used in URLs.

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.