开发者

What does a dot mean in a URL path?

开发者 https://www.devze.com 2023-03-05 20:09 出处:网络
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 w开发者_高级运维hat a dot actually does

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 w开发者_高级运维hat 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.


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.


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.


Now for a Simpler Explanation...

. and .. are NOT equivalent!

./ and ../ are NOT equivalent!

. and ./ ARE equivalent to

In all cases, . and ./ are the same as or "" or no path so not needed or used on the Web.

. (dot) is a relic of old UNIX pathing systems and NOT used on the World Wide Web for creating paths! Why? Because the dot in paths is redundant and equivalent to "" or no path or the current file directory you are in. The same result applies to using ./. It is the same as "" or no path. Both just reference the local directory your file is in ("./webpage.html" = "webpage.html").

What Path Should I Use?

  1. So NEVER use . or ./ as both paths are irrelevant!
  2. ALWAYS use ../ which is a RELATIVE PATH and says go up one folder.
  3. ALWAYS use / which is an ABSOLUTE PATH and says starts from the website root folder.

Want More Proof?

Check out the result for these image paths. Assume you are referencing these paths from an HTML web page stored in the root of the web site:

SUCCESSFUL PATHS ("../" and "/" paths work well on the Web)
<img src="../images/photo.jpg" />
<img src="/images/photo.jpg" />


REDUNDANT PATHS ("." not needed)
<img src="/images/./photo.jpg" />
...same as...
<img src="/images/photo.jpg" />


<img src="/images./photo.jpg" />
...same as...
<img src="/images/photo.jpg" />


FAILED PATHS ("." in paths that fail on the Web)
<img src="/images/.photo.jpg" />
<img src="./images/photo.jpg" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号