将相对链接转换为绝对? [英] Converting Relative links to Absolute?

查看:338
本文介绍了将相对链接转换为绝对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am programming a small script using PHP and regular expressions

此脚本的目的是提取页面中的所有链接并将这些链接转换为absolute- when这是相对的 -
我想到了相对链接是如何工作的,但它们也是一些问题

The aim of this script is to extract all links in a page and convert these links to absolute- when it is relative - I figured how does relative links works but their are some questions also

我们可以说我们有这个页面 http://www.example.com/xxx1/xxx2/xxx3.html
if this page有以下链接

lets say we have this page http://www.example.com/xxx1/xxx2/xxx3.html if this page has the following links

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

index.html --- the absolute link will be http://www.example.com/xxx1/xxx2/index.html

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

./index.html --- the absolute link will be http://www.example.com/xxx1/xxx2/index.html

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

../index.html --- the absolute link will be http://www.example.com/xxx1/index.html

/index.html ---绝对链接将是< a href =http://www.example.com/index.html =nofollow noreferrer> http://www.example.com/index.html

/index.html --- the absolute link will be http://www.example.com/index.html

所以

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

index.html = will open in the current directory

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

./index.html = will also open in the current directory

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

../index.html = will open in the parent directory

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

/index.html = will open in the root directory

问题是如果URL是搜索引擎友好的?

the problem is what if the URL is a search engine friendly?

说我们有这个网址

((case1)): http://www.example.com/xxx1/xxx2/xxx3/index/

((case1)): http://www.example.com/xxx1/xxx2/xxx3/index/

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

是inde x在case1中是一个目录还是一个页面?是case2中的目录还是一个页面?
以及以下链接在第1和第2种案例中的绝对链接如何显示

is "index" in case1 a directory or a page?is it a directory in case2 or a page? and how the following links will look like as absolute links in both cases 1 and 2

index.html ---?

index.html --- ?

./ index.html ---?

./index.html --- ?

../ index.html ---?

../index.html --- ?

/index.html ---?

/index.html --- ?

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

I am not sure if it is an easy question for some of you but for me it is confusing?

谢谢:)

推荐答案

直接回答你的例子



案例1 中, index 是URL的目录组件,而在案例2 索引是URL的文件组件。这与它实际上是Web服务器上的常规文件还是目录无关 - 请参阅下面的说明。如果服务器在这些网址上投放HTML页面,我会称之为页面。

Direct answer to your example

In case 1, index is a "directory component" of the URL, while in case 2 index is a "file component" of the URL. This is independent of whether it actually is a regular file or directory on the web server -- see the explanation below. I'd call both a "page" if an HTML page is served by the server on those URLs.

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

Case 1: (Links from 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

  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

Case 2: (Links from 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

  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。

So the only one that stays the same is the absolute links - 4.

链接是相对于浏览器所在的URL,可能不是URL您最初输入的内容(例如,在HTTP重定向上)。一旦您关注链接或被重定向,大多数Web浏览器都会使用当前地址更新URL栏,因此,除非您刚刚编辑了该地址,否则您看到的地址就是重要的地址。

Links are relative to the URL the browser is at, which may not be the URL you originally entered (for example on an HTTP redirect). Most web browsers will update the URL bar with the current address once you follow a link or are redirected, so unless you just edited that, the address you see there is the one that counts.

以斜杠结尾的网址被视为引用目录( RFC2396 对于URI语法,虽然它实际上并没有这样调用它们,否则它们被认为是指目录中的文件。

URLs ending with a slash are considered to refer to directories (implied by RFC2396 for URI syntax, though it does not actually call them that way), else they are considered to refer to files within directories.

- 旁注: 这不一定对应于Web服务器用于提供文件的文件系统路径(如果有)。大多数Web服务器,当被要求请求URL映射到目录时在他们的文件系统上,将使用一些设置名称(通常为index.html,但通常可以配置选择)来提供目录中的文件,或者服务器生成的HTML目录列表(如果禁用了访问错误) 。当请求没有尾部斜杠的类似路径的文件URL时,通常会提供相同的操作,在这种情况下,文件URL实际上映射到目录文件系统路径。 -

--Side note: This will not necessarily correspond to the filesystem path (if there is one) type used by the web server to serve the file. Most web servers, when asked requested a URL mapping to a directory on their filesystem, will either serve a file within the directory with some set name (often index.html, but the selection can usually be configured), or an HTML directory listing generated by the server (or an access error if that was disabled). The same will usually be served when a "file URL" for the similiar path without a trailing slash is requested, in which case the "file URL" actually maps to a directory filesystem path.--

这可能导致不一致,例如上面的例子,其中文件URL http://www.example.com/xxx1/xxx2/xxx3/index 可能相当于目录URL http://www.example.com/ xxx1 / xxx2 / xxx3 / index / ,但相对链接可能引用这两个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.

因此,当链接到目录时,建议始终使用目录URL(带有终止斜杠)而不是等效的文件URL - 例如链接到 http://www.ietf.org/meetings/ 而不是 http://www.ietf.org/meetings 即使两者都服务于同一页面。事实上,许多Web服务器都配置为使用HTTP 301重定向响应将请求后者的客户端重定向到前者。如果您在浏览器的URL栏中输入后者,您可以看到这一点 - 一旦获得该响应,URL栏就会变为前者。

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.

这篇关于将相对链接转换为绝对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆