我想知道这两种类型的url之间的区别:相对url(图片,CSS文件,JS文件等)和绝对url。
另外,用哪个比较好呢?
我想知道这两种类型的url之间的区别:相对url(图片,CSS文件,JS文件等)和绝对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">
... 但这只是提高速度的最佳做法。在你的情况下,你看起来不像这样做,所以我不会担心。
其他回答
一般来说,使用相对URL被认为是最佳实践,这样你的网站就不会被绑定到当前部署的基础URL。例如,它可以在本地主机上工作,也可以在您的公共域上工作,而无需修改。
我衷心推荐使用相对url将同一站点的部分指向同一站点的其他部分。
不要忘记,即使在同一个站点中,更改到HTTPS也需要一个绝对URL。
在大多数情况下,相对url是可行的,它们本质上是可移植的,这意味着如果您想提升您的站点并将其放在其他地方,它将立即工作,可能会减少调试时间。
有一篇相当不错的文章是关于绝对url和相对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处理通常对您更有好处,这样您就可以真正利用相对链接的好处。
假设我们正在创建一个子站点,其文件位于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)。