我想知道这两种类型的url之间的区别:相对url(图片,CSS文件,JS文件等)和绝对url。

另外,用哪个比较好呢?


当前回答

对于支持相对URI解析的每个系统,相对URI和绝对URI都服务于同一个目标:引用。它们可以互换使用。所以你可以在每种情况下做出不同的决定。从技术上讲,它们提供相同的引用。

确切地说,每个相对URI都有一个绝对URI。那是那个相对URI被解析的base-URI。所以相对URI实际上是绝对URI之上的一个特性。

这也是为什么使用相对URI可以比单独使用绝对URI做得更多的原因——这对于静态网站尤其重要,否则与绝对URI相比,静态网站的维护就不那么灵活。

These positive effects of relative URI resolution can be exploited for dynamic web-application development as well. The inflexibility absolute URIs do introduce are also easier to cope up with, in a dynamic environment, so for some developers that are unsure about URI resolution and how to properly implement and manage it (not that it's always easy) do often opt into using absolute URIs in a dynamic part of a website as they can introduce other dynamic features (e.g. configuration variable containing the URI prefix) so to work around the inflexibility.

So what is the benefit then in using absolute URIs? Technically there ain't, but one I'd say: Relative URIs are more complex because they need to be resolved against the so called absolute base-URI. Even the resolution is strictly define since years, you might run over a client that has a mistake in URI resolution. As absolute URIs do not need any resolution, using absolute URIs have no risk to run into faulty client behaviour with relative URI resolution. So how high is that risk actually? Well, it's very rare. I only know about one Internet browser that had an issue with relative URI resolution. And that was not generally but only in a very (obscure) case.

与HTTP客户端(浏览器)相比,对于超文本文档或代码的作者来说,它可能更加复杂。在这里,绝对URI的好处是更容易测试,因为您只需将其原样输入到浏览器的地址栏中。然而,如果这不仅仅是您一个小时的工作,那么实际理解绝对和相对URI处理通常对您更有好处,这样您就可以真正利用相对链接的好处。

其他回答

如果是在你的网站中使用,最好使用相对URL,如果你需要将网站移动到另一个域名或只是在本地调试,你可以这样做。

看看stackoverflow在做什么(firefox中的ctrl+U):

<a href="/users/recent/90691"> // Link to an internal element

在某些情况下,他们使用绝对url:

<link rel="stylesheet" href="http://sstatic.net/so/all.css?v=5934">

... 但这只是提高速度的最佳做法。在你的情况下,你看起来不像这样做,所以我不会担心。

假设您有一个网站www.yourserver.example。在web文档的根目录中有一个images子目录,其中有myimage.jpg。

绝对URL定义了文档的确切位置,例如:

http://www.yourserver.example/images/myimage.jpg

相对URL定义了相对于当前目录的位置,例如,假设你在你的图片所在的根目录中:

images/myimage.jpg

(相对于根目录)

你应该尽可能使用相对url。如果你将网站移动到www.anotherserver.com,你将不得不更新所有指向www.yourserver.example的绝对url,相对的url将继续工作。

实际上有三种类型需要明确讨论。在实践中,虽然URL已经被抽象为在较低的级别上处理,但我想说的是,开发人员可以一辈子都不用手写一个URL。

绝对

绝对url将代码绑定到协议和域。这可以通过动态url解决。

<a href=“https://dev.example.com/a.html?q=”>https://dev.example.com/a.html?q=</a>

绝对的优点:

Control - The subdomain and protocol can be controlled. People that enter through an obscure subdomain will be funneled into the proper subdomain. You can hop back and forth between secure and non-secure as appropriate. Configurable - Developers love things to be absolute. You can design neat algorithms when using absolute URLs. URLs can be made configurable so that a URL can be updated site-wide with a single change in a single configuration file. Clairvoyance - You can search for the people scraping your site or maybe pick up some extra external links.


根相对

根相对url将您的代码绑定到基url。这可以通过动态url和/或基本标记来克服。

<a href=“/index.php?q=”>.example.com/index.php?q=</a>

根相对优点:

可配置的-基标签使它们相对于你选择的任何根,使切换域和实现模板变得容易。


相对

相对url将代码绑定到目录结构。这是无法克服的。相对url仅在文件系统中用于遍历目录或作为低级任务的快捷方式。

<a href=“index.php?q=”>index.php?q=</a>
<link src=“../.././../css/default.css” />

相对的缺点:

CONFUSING - How many dots is that? how many folders is that? Where is the file? Why isn't it working? MAINTENANCE - If a file is accidentally moved resources quit loading, links send the user to the wrong pages, form data might be sent to the incorrect page. If a file NEEDS to be moved all the resources that are going to quit loading and all the links that are going to be incorrect need to be updated. DOES NOT SCALE - When webpages become more complex and views start getting reused across multiple pages the relative links will be relative to the file that they were included into. If you have a navigation snippet of HTML that is going to be on every page then relative will be relative to a lot of different places. The first thing people realize when they start creating a template is that they need a way to manage the URLs. COMPUTED - They are implemented by your browser (hopefully according to RFC). See chapter 5 in RFC3986. OOPS! - Errors or typos can result in spider traps.


路线的演变

开发人员已经停止编写这里所讨论的url。所有的请求都是针对一个网站的索引文件,并包含一个查询字符串,也就是路由。路由可以看作是一个迷你URL,它告诉应用程序要生成的内容。

<a href="<?=Route::url('named_url', array('first' => 'my', 'last' => 'whacky'))?>">
    http://dev.example.com/index.php/my:whacky:url
</a>

路线的优点:

绝对url的所有优点。 URL中任意字符的使用。 更多的控制(有利于搜索引擎优化)。 能够算法生成url。这允许url是可配置的。更改URL是对单个文件的单个更改。 不需要404 not found。回退路由可以显示站点地图或错误页面。 方便安全的间接访问应用程序文件。警卫声明可以确保每个人都是通过适当的渠道到达的。 MVC方法的实用性。


我的看法

大多数人会在他们的项目中以某种方式使用这三种形式。关键是要了解它们,并选择最适合这项任务的一个。

以URL方案和方案特定部分(http://, https://, ftp://,等)开头的URL是绝对URL。

任何其他URL都是相对URL,并且需要一个基本URL,相对URL是从引用所使用的资源的URL中解析出来的(因此依赖于),如果没有声明的话。

请参阅RFC 2396 -附录C中有关解析相对url的示例。

对于支持相对URI解析的每个系统,相对URI和绝对URI都服务于同一个目标:引用。它们可以互换使用。所以你可以在每种情况下做出不同的决定。从技术上讲,它们提供相同的引用。

确切地说,每个相对URI都有一个绝对URI。那是那个相对URI被解析的base-URI。所以相对URI实际上是绝对URI之上的一个特性。

这也是为什么使用相对URI可以比单独使用绝对URI做得更多的原因——这对于静态网站尤其重要,否则与绝对URI相比,静态网站的维护就不那么灵活。

These positive effects of relative URI resolution can be exploited for dynamic web-application development as well. The inflexibility absolute URIs do introduce are also easier to cope up with, in a dynamic environment, so for some developers that are unsure about URI resolution and how to properly implement and manage it (not that it's always easy) do often opt into using absolute URIs in a dynamic part of a website as they can introduce other dynamic features (e.g. configuration variable containing the URI prefix) so to work around the inflexibility.

So what is the benefit then in using absolute URIs? Technically there ain't, but one I'd say: Relative URIs are more complex because they need to be resolved against the so called absolute base-URI. Even the resolution is strictly define since years, you might run over a client that has a mistake in URI resolution. As absolute URIs do not need any resolution, using absolute URIs have no risk to run into faulty client behaviour with relative URI resolution. So how high is that risk actually? Well, it's very rare. I only know about one Internet browser that had an issue with relative URI resolution. And that was not generally but only in a very (obscure) case.

与HTTP客户端(浏览器)相比,对于超文本文档或代码的作者来说,它可能更加复杂。在这里,绝对URI的好处是更容易测试,因为您只需将其原样输入到浏览器的地址栏中。然而,如果这不仅仅是您一个小时的工作,那么实际理解绝对和相对URI处理通常对您更有好处,这样您就可以真正利用相对链接的好处。