直接回答你的例子
在情况 1中,index
是 URL 的“目录组件”,而在情况 2 index
中,是 URL 的“文件组件”。这与它实际上是 Web 服务器上的常规文件还是目录无关——请参阅下面的说明。如果服务器在这些 URL 上提供 HTML 页面,我将两者都称为“页面”。
案例 1:( 来自 的链接http://www.example.com/xxx1/xxx2/xxx3/index/
)
index.html
->http://www.example.com/xxx1/xxx2/xxx3/index/index.html
./index.html
->http://www.example.com/xxx1/xxx2/xxx3/index/index.html
../index.html
->http://www.example.com/xxx1/xxx2/xxx3/index.html
/index.html
->http://www.example.com/index.html
案例 2:( 来自 的链接http://www.example.com/xxx1/xxx2/xxx3/index
)
index.html
->http://www.example.com/xxx1/xxx2/xxx3/index.html
./index.html
->http://www.example.com/xxx1/xxx2/xxx3/index.html
../index.html
->http://www.example.com/xxx1/xxx2/index.html
/index.html
->http://www.example.com/index.html
所以唯一保持不变的是绝对链接 - 4。
解释
链接相对于浏览器所在的 URL,它可能不是您最初输入的 URL(例如在 HTTP 重定向中)。一旦您点击链接或被重定向,大多数网络浏览器都会使用当前地址更新 URL 栏,因此除非您只是编辑了该地址,否则您看到的地址才是最重要的。
以斜杠结尾的 URL 被认为是指目录(由RFC2396对 URI 语法进行暗示,尽管它实际上并不这样称呼它们),否则它们被认为是指目录中的文件。
--旁注: 这不一定对应于Web服务器用于提供文件的文件系统路径(如果有的话)类型。 大多数 Web 服务器,当被要求提供到其文件系统上的目录的 URL 映射时,将在目录中提供具有某些设置名称的文件(通常是 index.html,但通常可以配置选择),或者生成 HTML 目录列表由服务器(如果禁用,则访问错误)。当请求没有尾部斜杠的类似路径的“文件 URL”时,通常会提供相同的服务,在这种情况下,“文件 URL”实际上映射到目录文件系统路径。--
This can lead to inconsistencies such as the above example, where the "file URL" http://www.example.com/xxx1/xxx2/xxx3/index
is probably equivalent to the "directory URL" http://www.example.com/xxx1/xxx2/xxx3/index/
, but relative links may refer to different paths from those two URLs, and one may work and the other may be broken.
For that reason, when a linking to a directory, it is recommended to always use the "directory URL" (with the terminating slash) and not the equivalent "file URL" - e.g. link to http://www.ietf.org/meetings/
and not http://www.ietf.org/meetings
even if both would serve the same page. Many web servers are in fact configured to redirect clients requesting the latter to the former using a an HTTP 301 redirect response. You can see this if you enter the latter in your browser's URL bar - the URL bar will change to the former once it gets that response.