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

另外,用哪个比较好呢?


当前回答

我应该使用绝对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被认为是最佳实践,这样你的网站就不会被绑定到当前部署的基础URL。例如,它可以在本地主机上工作,也可以在您的公共域上工作,而无需修改。

在大多数情况下,相对url是可行的,它们本质上是可移植的,这意味着如果您想提升您的站点并将其放在其他地方,它将立即工作,可能会减少调试时间。

有一篇相当不错的文章是关于绝对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方法的实用性。


我的看法

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

假设我们正在创建一个子站点,其文件位于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)。

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