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

另外,用哪个比较好呢?


当前回答

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

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

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

其他回答

假设您有一个网站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将继续工作。

假设我们正在创建一个子站点,其文件位于http://site.ru/shop文件夹中。

1. 绝对URL

Link to home page
href="http://sites.ru/shop/"

Link to the product page
href="http://sites.ru/shop/t-shirts/t-shirt-life-is-good/"

2. 相对URL

Link from home page to product page
href="t-shirts/t-shirt-life-is-good/"

Link from product page to home page
href="../../"

虽然相对URL看起来比绝对URL短,但绝对URL更可取,因为链接可以在网站的任何页面上使用。

中间的情况下

我们考虑了两种极端情况:“绝对”绝对url和“绝对”相对url。但在这个世界上,一切都是相对的。这也适用于url。每次提到绝对URL时,都应该指定相对于什么。

3.Protocol-relative URL

Link to home page
href="//sites.ru/shop/"

Link to product page
href="//sites.ru/shop/t-shirts/t-shirt-life-is-good/"

谷歌推荐这样的URL。然而,现在通常认为http://和https://是不同的网站。

4. Root-relative URL

即相对于域的根文件夹。

Link to home page
href="/shop/"

Link to product page
href="/shop/t-shirts/t-shirt-life-is-good/"

如果所有页面都在同一个域中,这是一个很好的选择。当你把你的网站转移到另一个域名时,你不必在url中做大量的域名替换。

5. 基本相对URL(主页相对)

标签<base>指定基本URL,它会自动添加到所有相对链接和锚点。base标记不影响绝对链接。作为基本URL,我们将指定主页:<base href="http://sites.ru/shop/">。

Link to home page
href=""

Link to product page
href="t-shirts/t-shirt-life-is-good/"

现在,您不仅可以移动您的网站到任何域,但在任何子文件夹。请记住,虽然url看起来像相对的,但实际上它们是绝对的。 尤其要注意锚。要在当前页面中导航,我们必须写入href="t-shirt /t-shirt-life-is-good/#comments"而不是href="#comments"。后者将扔在首页上。

结论

对于内部链接,我使用基本相对url(5),对于外部链接和时事通讯,我使用绝对url(1)。

对于支持相对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方案是“好的”,当你想快速启动和运行一些东西,而不是跳出框框思考,特别是如果你的项目很小,只有几个开发人员(或者只有你自己)。

然而,一旦您开始在大型的、臃肿的系统上工作,并且一直在切换域和协议,我相信一种更优雅的方法是合适的。

当你从本质上比较绝对和相对url时,绝对url胜出。为什么?因为它永远不会坏。永远。绝对URL就是它所描述的那样。问题是你必须维护你的绝对url。

The weak approach to absolute URL linking is actually hard coding the entire URL. Not a great idea, and probably the culprit of why people see them as dangerous/evil/annoying to maintain. A better approach is to write yourself an easy to use URL generator. These are easy to write, and can be incredibly powerful- automatically detecting your protocol, easy to config (literally set the url once for the whole app), etc, and it injects your domain all by itself. The nice thing about that: You go on coding using relative URLs, and at run time the application inserts your URLs as full absolutes on the fly. Awesome.

鉴于所有现代网站实际上都使用某种动态后端,这样做对网站最有利。绝对url的作用不仅仅是让你确定它们指向哪里——它们还可以提高SEO性能。

I might add that the argument that absolute URLs is somehow going to change the load time of the page is a myth. If your domain weighs more than a few bytes and you're on a dialup modem in the 1980s, sure. But that's just not the case anymore. https://stackoverflow.com/ is 25 bytes, whereas the "topbar-sprite.png" file that they use for the nav area of the site weighs in at 9+ kb. That means that the additional URL data is .2% of the loaded data in comparison to the sprite file, and that file is not even considered a big performance hit.

大的,未优化的,整个页面的背景图像更有可能减慢你的加载时间。

这里有一篇关于为什么不应该使用相对url的有趣文章: 为什么相对url应该被禁止

例如,可能会出现的一个问题是,有时服务器映射(请注意大型、混乱的项目)与文件名不一致,开发人员可能会对相对URL做出不正确的假设。我今天在我参与的一个项目上看到了这个,它把整个页面都拖下来了。

或者开发人员忘记切换指针,突然谷歌索引了整个测试环境。唉,重复的内容(不利于搜索引擎优化!)

绝对可能是危险的,但如果使用得当,并且不会破坏你的构建,它们被证明是更可靠的。看看上面的文章,它给出了一堆为什么Wordpress url生成器超级棒的理由。

:)

如果是在你的网站中使用,最好使用相对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">

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