我想知道这两种类型的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方案和方案特定部分(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处理通常对您更有好处,这样您就可以真正利用相对链接的好处。

我应该使用绝对url还是相对url ?

如果你所说的绝对url指的是包含方案(例如HTTP / HTTPS)和主机名(例如yourdomain.example)的url,不要这样做(对于本地资源),因为维护和调试会很糟糕。

假设您在代码中处处使用绝对URL,如<img src="http://yourdomain.example/images/example.png">。现在,当你要去的时候,会发生什么:

切换到其他方案(例如HTTP -> HTTPS) 切换域名(test.yourdomain. com)。示例-> yourdomain.example)

在第一个例子中,您将收到关于页面上正在请求的不安全内容的警告。因为你所有的url都是硬编码使用http(://yourdomain.example/images/example.png)。当通过HTTPS运行页面时,浏览器期望所有资源都通过HTTPS加载,以防止信息泄露。

在第二个例子中,当你把你的网站从测试环境中上线时,这意味着所有的资源仍然指向你的测试域,而不是你的上线域。

因此,为了回答您关于使用绝对url还是相对url的问题:始终使用相对url(用于本地资源)。

不同的url之间有什么区别?

首先让我们来看看我们可以使用的不同类型的url:

http://yourdomain.example/images/example.png / / yourdomain.example /图片/ example.png /图片/ example.png 图片/ example.png

这些url试图访问服务器上的哪些资源?

在下面的例子中,我假设网站在服务器/var/www/mywebsite上运行

http://yourdomain.example/images/example.png

上面的(绝对)URL试图访问资源/var/www/website/images/example.png。这种类型的URL是你总是想要避免从你自己的网站请求资源的原因如上所述。然而,它确实有它的位置。例如,如果你有一个网站http://yourdomain.example,你想通过HTTPS从外部域请求资源,你应该使用这个。例如,https://externalsite.example/path/to/image.png。

/ / yourdomain.example /图片/ example.png

这个URL是相对的,基于当前使用的方案,当包含外部资源(图像,javascript等)时几乎总是应该使用。

What this type of URL does is use the current scheme of the page it is on. This means that you are on the page http://yourdomain.example and on that page is an image tag <img src="//yourdomain.example/images/example.png"> the URL of the image would resolve in http://yourdomain.example/images/example.png. When you would have been on the page https://yourdomain.example and on that page is an image tag <img src="//yourdomain.example/images/example.png"> the URL of the image would resolve in https://yourdomain.example/images/example.png.

这可以防止在不需要时通过HTTPS加载资源,并自动确保在需要时通过HTTPS请求资源。

上面的URL在服务器端的解析方式与前面的URL相同:

上面的(绝对)URL试图访问资源/var/www/website/images/example.png。

/图片/ example.png

对于本地资源,这是首选的引用方式。这是一个基于你网站的文档根目录(/var/www/mywebsite)的相对URL。这意味着当你有<img src="/images/example.png">时,它总是会解析为/var/www/mywebsite/images/example.png。

如果在某个时候你决定切换域,它仍然可以工作,因为它是相对的。

图片/ example.png

这也是一个相对URL,尽管与前一个略有不同。这个URL相对于当前路径。这意味着它将解析到不同的路径,这取决于您在站点中的位置。

例如,当你在http://yourdomain.example页面上,你使用<img src="images/example.png">时,它会在服务器上解析为/var/www/mywebsite/images/example.png,然而,当你在http://yourdomain.example/some/path页面上,你使用完全相同的图像标记时,它会突然解析为/var/www/mywebsite/ somepath /images/example.png。

什么时候用什么?

在请求外部资源时,您很可能希望使用相对于方案的URL(除非您希望强制使用不同的方案),而在处理本地资源时,您希望使用基于文档根的相对URL。

文档示例:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
        <link href='//fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700' rel='stylesheet' type='text/css'>
        <link href="/style/style.css" rel="stylesheet" type="text/css" media="screen"></style>
    </head>
    <body>
        <img src="/images/some/localimage.png" alt="">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
    </body>
</html>

有些(有点)复制品

编写跨环境传输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。例如,它可以在本地主机上工作,也可以在您的公共域上工作,而无需修改。