URL 路径中的点是什么意思?

发布于 2024-11-07 12:12:23 字数 384 浏览 2 评论 0原文

有关 jQuery Ajax 问题的问题中,提问者试图在相对 URL 的开头使用 .。我建议他把它去掉,但不知道那里的点实际上有什么作用。

他的相对 URL 如下所示:

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

我尝试查找 RFC,但没有成功。我知道点在命令行(Linux 或 Win)中的作用,它代表当前目录。

我想知道:这在互联网上的 URL 中是如何工作的?它有任何最佳实践用途吗?非常欢迎详细的解释。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月打碎记忆 2024-11-14 12:12:23

通常使用 路径段 .在相对路径引用的开头,并在 引用解析,即将相对URI引用解析为绝对URI的过程:

路径段“.”和“..”,也称为点段,是
为路径名层次结构中的相对引用而定义。他们
用于相对路径引用的开头
(第 4.2 节) 指示层次结构中的相对位置
名字树。这类似于他们在某些运营中的角色
系统的文件目录结构来指示当前目录
和父目录。但是,与文件中不同
系统中,这些点段仅在 URI 路径内解释
层次结构并作为解析过程的一部分被删除(部分
5.2
)。

删除点段算法 描述了如何在特定的基本路径上下文中解释这些点段。

在您的情况下, ./delete-misc/test-ajax-code.phpdelete-misc/test-ajax-code.php 是等效的。但在某些情况下,相对路径可能会被误解为绝对 URI,例如,第一个路径段中的 :search:foo 不同。 ./search:foo 因为前者是绝对 URI,而后者是相对 URI 路径。

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.

娇俏 2024-11-14 12:12:23

URL 前面的 ./ 相当于当前路径。所以 ./delete-misc/test-ajax-code.phpdelete-misc/text-ajax-code.php 都是相对路径。在您发布的答案中,您要求仅删除点,因此 /delete-misc/test-ajax-code.php 的路径将转换为绝对路径而不是相对路径。

编辑:还有一件事 - . 是当前目录,.. 是父目录。正如 phihag 评论的那样,这些确实应该在代码中避免和保护。目录遍历可用于作恶。

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.

寂寞美少年 2024-11-14 12:12:23

现在进行更简单的解释...

... 并不等效!

./../ 不相等!

../ ARE 等同于

在所有情况下,../ 或“”相同,或者没有路径,因此不需要或在 Web 上使用。

.(点)是旧 UNIX 路径系统的遗物,不用于万维网上创建路径!为什么?因为路径中的点是多余的,相当于“”或无路径或当前所在的文件目录。相同的结果适用于使用./< /代码>。它与“”或无路径相同。两者都只是引用文件所在的本地目录(“./webpage.html”=“webpage.html”)。

我应该使用什么路径?

  1. 所以永远不要使用../,因为这两个路径是不相关的!
  2. 始终使用 ../ 这是一个相对路径,表示向上一个文件夹。
  3. 始终使用 / 这是绝对路径,表示从网站根文件夹开始。

想要更多证据?

查看这些图像路径的结果。假设您从存储在网站根目录中的 HTML 网页引用这些路径:

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" />

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文