1

我正在使用 PHP 和正则表达式编写一个小脚本

该脚本的目的是提取页面中的所有链接并将这些链接转换为绝对链接——当它是相对链接时——我知道相对链接是如何工作的,但它们也是一些问题

假设我们有这个页面http://www.example.com/xxx1/xxx2/xxx3.html 如果这个页面有以下链接

index.html --- 绝对链接是http://www.example.com/xxx1/xxx2/index.html

./index.html --- 绝对链接是http://www.example.com/xxx1/xxx2/index.html

../index.html --- 绝对链接是http://www.example.com/xxx1/index.html

/index.html --- 绝对链接是http://www.example.com/index.html

所以

index.html = 将在当前目录中打开

./index.html = 也会在当前目录打开

../index.html = 将在父目录中打开

/index.html = 将在根目录中打开

问题是如果 URL 对搜索引擎友好怎么办?

说我们有这个网址

((案例1)):http ://www.example.com/xxx1/xxx2/xxx3/index/

或者

((案例2)):http ://www.example.com/xxx1/xxx2/xxx3/index

case1 中的“索引”是目录还是页面?它是 case2 中的目录还是页面?以及在情况 1 和 2 中以下链接如何作为绝对链接

index.html --- ?

./index.html --- ?

../index.html --- ?

/index.html --- ?

我不确定这对你们中的一些人来说是不是一个简单的问题,但对我来说它是令人困惑的?

谢谢 :)

4

1 回答 1

3

直接回答你的例子

情况 1中,index是 URL 的“目录组件”,而在情况 2 index中,是 URL 的“文件组件”。这与它实际上是 Web 服务器上的常规文件还是目录无关——请参阅下面的说明。如果服务器在这些 URL 上提供 HTML 页面,我将两者都称为“页面”。

案例 1:( 来自 的链接http://www.example.com/xxx1/xxx2/xxx3/index/

  1. index.html->http://www.example.com/xxx1/xxx2/xxx3/index/index.html
  2. ./index.html->http://www.example.com/xxx1/xxx2/xxx3/index/index.html
  3. ../index.html->http://www.example.com/xxx1/xxx2/xxx3/index.html
  4. /index.html->http://www.example.com/index.html

案例 2:( 来自 的链接http://www.example.com/xxx1/xxx2/xxx3/index

  1. index.html->http://www.example.com/xxx1/xxx2/xxx3/index.html
  2. ./index.html->http://www.example.com/xxx1/xxx2/xxx3/index.html
  3. ../index.html->http://www.example.com/xxx1/xxx2/index.html
  4. /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.

于 2009-02-13T08:57:13.233 回答