39

In a question regarding a jQuery Ajax problem, the asker was trying to use a . in the beginning of a relative URL. I advised him to remove it, but have no idea what a dot actually does there.

His relative URL looked like this:

./delete-misc/test-ajax-code.php

I tried looking in the RFCs, without success. I know what the dot does in command line (either Linux or Win), it represents the current directory.

I'd like to know: how does this work on the Internet in a URL? Does it have any best-practice uses? Detailed explanations are most welcome.

Community
  • 1
  • 1
kapa
  • 75,446
  • 20
  • 155
  • 173

2 Answers2

41

The path segment . is typically used at the begin of relative path references and are removed during the reference resolution, i.e. the process of resolving a relative URI reference to an absolute URI:

The path segments "." and "..", also known as dot-segments, are defined for relative reference within the path name hierarchy. They are intended for use at the beginning of a relative-path reference (Section 4.2) to indicate relative position within the hierarchical tree of names. This is similar to their role within some operating systems' file directory structures to indicate the current directory and parent directory, respectively. However, unlike in a file system, these dot-segments are only interpreted within the URI path hierarchy and are removed as part of the resolution process (Section 5.2).

There is Remove Dot Segments algorithms that describes how these dot segments are to be interpreted in a certain base path context.

In your case both ./delete-misc/test-ajax-code.php and delete-misc/test-ajax-code.php are equivalent. But there are cases where a relative path can be misinterpreted as an absolute URI, e.g. having a : in the first path segment like search:foo that is different to ./search:foo as the former is an absolute URI while the latter is a relative URI path.

Community
  • 1
  • 1
Gumbo
  • 620,600
  • 104
  • 758
  • 828
  • Thank you, exactly what I was looking for. Thanks for the links. – kapa May 15 '11 at 14:41
  • It appears that prepending "." makes browser respect value of the base tag (e.g ``) – sad comrade Apr 19 '17 at 12:44
  • 2
    warning: if your system serves the same content at this url: `http://example.com/folder` and this url: `http://example.com/folder/` then a url on that page of `./page` will go to `http://example.com/page` and `http://example.com/folder/page` respectively. – Myster Mar 07 '18 at 03:33
  • 1
    @Chintsu That's simply because the dot prefix makes the URL-path _relative_ - as mentioned above. Whereas a URL of the form `/some/path/` is _root-relative_ (incidentally, both respect the base tag - but behaviour is naturally different). `./some/path/` (with dot prefix) and `some/path` (relative path, without `./`) are the same. – MrWhite Jul 03 '20 at 14:10
7

A ./ in front of the URL is equivalent to the current path. So ./delete-misc/test-ajax-code.php and delete-misc/text-ajax-code.php are both relative paths. In the answer you posted, you asked to remove the dot only, so the path of /delete-misc/test-ajax-code.php would translate as an absolute path instead of a relative path.

Edit: one more thing - . is the current directory and .. is the parent directory. As phihag comments, these really should be avoided and protected against in code. Directory traversal can be used for evil.

kapa
  • 75,446
  • 20
  • 155
  • 173
Michael Dean
  • 1,416
  • 9
  • 11
  • I don't think your 3rd example is an absolute path. It is a relative path that is evaluated from the root (starts with `/`). – kapa May 15 '11 at 14:34
  • 2
    @bazmegakapa: It is an absolute path in terms of RFC 3986. – Gumbo May 15 '11 at 14:42
  • @kapa: I think the confusion might be between an "absolute URI" and an "absolute path" (or "path-absolute" as defined in RFC 3986). An "absolute-URI" is defined as `scheme ":" hier-part [ "?" query ]`, which this clearly is not. – MrWhite Dec 30 '13 at 09:44