Absolute to relative URL?
Jeff Shannon
jeff at ccvcorp.com
Thu Apr 4 14:52:57 EST 2002
In article <3CAC1665.8040209 at mxm.dk>, maxm at mxm.dk says...
> Thomas Guettler wrote:
>
>
> > Is there already a function in the standard python modules
> > which returns the a relative url given two absolute urls?
> >
> > Example:
> >
> > relative_url("http://foo/a/b/c", "http://foo/a/d")
> > --> return: "../d"
>
> I hope you mean that the result should be "../../d" ??
Actually, that's not quite correct (and neither are your code
examples below, for the same reason).
In the URL 'http://foo/a/b/c', we could presume that 'foo' is a
domain name, 'a' and 'b' are directories, and 'c' is a filename.
Thus, the 'current directory' ('.') for this URL is //foo/a/b,
and the parent directory ('..') is //foo/a. Therefore, in this
case, the O.P.'s initial relative URL ('../d') is correct.
The issue is complicated somewhat in that '//foo/a/b/c' can
*also* be shorthand for '//foo/a/b/c/index.html' (or whatever the
server's default-filename is). If (and *only* if) the original
URL is representing a default document, then your result would be
the correct one.
There doesn't seem to be a way to determine whether a given URL
represents a default document or not, however, without hitting
the server in question to find out. (I certainly wouldn't want
to rely on the type of, or even existence of, a file extension
for this purpose. No rule says that HTML documents *must* use a
.htm/.html extension...)
--
Jeff Shannon
Technician/Programmer
Credit International
More information about the Python-list
mailing list