then the base URL is considered to be the empty string and all
embedded URLs within that document are assumed to be absolute URLs.
RFC 1808 Relative Uniform Resource Locators June 1995
It is the responsibility of the distributor(s) of a document
containing relative URLs to ensure that the base URL for that
document can be established. It must be emphasized that relative
URLs cannot be used reliably in situations where the document's base
URL is not well-defined.
4. Resolving Relative URLs
This section describes an example algorithm for resolving URLs within
a context in which the URLs may be relative, such that the result is
always a URL in absolute form. Although this algorithm cannot
guarantee that the resulting URL will equal that intended by the
original author, it does guarantee that any valid URL (relative or
absolute) can be consistently transformed to an absolute form given a
valid base URL.
The following steps are performed in order:
Step 1: The base URL is established according to the rules of
Section 3. If the base URL is the empty string (unknown),
the embedded URL is interpreted as an absolute URL and
we are done.
Step 2: Both the base and embedded URLs are parsed into their
component parts as described in Section 2.4.
a) If the embedded URL is entirely empty, it inherits the
entire base URL (i.e., is set equal to the base URL)
and we are done.
b) If the embedded URL starts with a scheme name, it is
interpreted as an absolute URL and we are done.
c) Otherwise, the embedded URL inherits the scheme of
the base URL.
Step 3: If the embedded URL's is non-empty, we skip to
Step 7. Otherwise, the embedded URL inherits the
(if any) of the base URL.
Step 4: If the embedded URL path is preceded by a slash "/", the
path is not relative and we skip to Step 7.
RFC 1808 Relative Uniform Resource Locators June 1995
Step 5: If the embedded URL path is empty (and not preceded by a
slash), then the embedded URL inherits the base URL path,
and
a) if the embedded URL's is non-empty, we skip to
step 7; otherwise, it inherits the of the base
URL (if any) and
b) if the embedded URL's is non-empty, we skip to
step 7; otherwise, it inherits the of the base
URL (if any) and we skip to step 7.
Step 6: The last segment of the base URL's path (anything
following the rightmost slash "/", or the entire path if no
slash is present) is removed and the embedded URL's path is
appended in its place. The following operations are
then applied, in order, to the new path:
a) All occurrences of "./", where "." is a complete path
segment, are removed.
b) If the path ends with "." as a complete path segment,
that "." is removed.
c) All occurrences of "/../", where is a
complete path segment not equal to "..", are removed.
Removal of these path segments is performed iteratively,
removing the leftmost matching pattern on each iteration,
until no matching pattern remains.
d) If the path ends with "/..", where is a
complete path segment not equal to "..", that
"/.." is removed.
=6= |