相对链接比站点链接快得多吗?

发布于 2025-01-06 16:42:44 字数 761 浏览 1 评论 0原文

在我的网站上,我倾向于使用站点链接而不是相对链接,如下所示:

我使用:

<link rel="stylesheet" type="text/css" href="http://www.example.com/_css/_all/stylesheet-global-styles.css" />

替代(相对):

<link rel="stylesheet" type="text/css" href="/_css/_all/stylesheet-global-styles.css" />

我使用站点链接的原因是因为我使用很多虚荣网址,例如:

example.com/test =>; example.com/test.php

example.com/test/1 => example.com/test.php?id=1

由于第二个示例中存在多个“/”,我的页面认为它们位于网站的子目录中,因此“相对”链接不起作用,除非我输入一个“../”。我不能这样做,因为这意味着目录基于查询字符串是否存在以及有多少个“/”。因此,我被迫使用网站链接来包含我所需的内容。

我知道每个请求都必须执行 DNS 查找,这会减慢速度,但我的问题是,如果我链接(比方说)5 个样式表和脚本,速度会慢多少?

On my websites I tend to use site links as opposed to relative links as shown below:

I use:

<link rel="stylesheet" type="text/css" href="http://www.example.com/_css/_all/stylesheet-global-styles.css" />

Alternative (relative):

<link rel="stylesheet" type="text/css" href="/_css/_all/stylesheet-global-styles.css" />

The reason I use site links is because I use a lot of vanity URL's such as:

example.com/test => example.com/test.php

example.com/test/1 => example.com/test.php?id=1

As a result of the multiple '/' within the second example, my pages think that they are in a subdirectory of the website so therefore 'relative' links do not work unless I put in a '../'. I cannot do this because that would mean directories based upon whether or not a query string exists and how many '/' there are. Consequently I am forced to use site links to include my required content.

I am aware that each request must therefore execute a DNS lookup and this will slow it down, but my question is, how much slower is it if I am linking, lets say, 5 stylesheets and scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

唯憾梦倾城 2025-01-13 16:42:45

这并没有什么区别。无论如何,浏览器都会在幕后将链接转换为完整的 URL。由于 DNS 查找被缓存,因此 DNS 查找成本最小。

恕我直言,相对链接更容易维护。您应该只使用您认为最方便的技术。性能在这里不是问题。

如果您想提高性能,最好将样式表合并到一个文件中并将其缩小。脚本文件也是如此。

It doesn’t really make a difference. Behind the scenes, browsers convert the links into full URLs anyway. The DNS lookup cost is minimal since DNS lookups are cached.

IMHO, relative links are easier to maintain. You should just use whichever technique you think is the most convenient. Performance is not an issue here.

If you want to improve performance, you’d be much better off combining your stylesheets into a single file and minifying it. The same goes for script files.

半山落雨半山空 2025-01-13 16:42:45

单击浏览器中的链接时会发生什么?
浏览器启动 HTTP 请求。

它看起来怎么样?
如果您位于 http://www.example.com/path1/page1.html

然后点击链接
../path2/page2.html
/path2/page2.html
http://www.example.com/path2/page2.html
请求始终相同

GET /path2/page2.html HTTP/1.1  
host: www.example.com

,因此与请求没有区别

,因此请随意组织您的链接。

What happens when you click a link in your browser?
The browser starts a HTTP request.

How does it look like?
If you are on http://www.example.com/path1/page1.html

and click on links
../path2/page2.html or
/path2/page2.html or
http://www.example.com/path2/page2.html
the request is always the same

GET /path2/page2.html HTTP/1.1  
host: www.example.com

So no difference with the request

So feel free to organize your links as you like.

辞别 2025-01-13 16:42:45

当在引用之前使用 / 时,您的路径将成为从网站根目录开始的绝对路径 - 与使用 ../ 相反,就像您所说的相对路径。

When using an / before the reference your path becomes absolute beginning from the root of your website - in opposite to using ../ which is like you said relative.

錯遇了你 2025-01-13 16:42:45

你为什么不这样做

//yourapp.com/assets/css/style.css
//yourapp.com/assets/js/jq.js

//yourapp.com/cp/register
//yourapp.com/cp/update_account

//字面意思是无论是SSL还是非SSL,它总是会解析。不
速度也有差异。

参考双斜杠域名 //site.com 以及 ssl 非 ssl

Why don't you do this

//yourapp.com/assets/css/style.css
//yourapp.com/assets/js/jq.js

//yourapp.com/cp/register
//yourapp.com/cp/update_account

// literally means if SSL or if non-SSL, it will always resolve. No
difference in speed either.

reference on double slash domain //site.com and for ssl non ssl

治碍 2025-01-13 16:42:44

相对链接比网站链接快得多吗?

不,因为它们是同一个。相对链接只是浏览器为网站管理员提供的便利。它们最终也被解析为绝对链接:发送到服务器的每个请求始终包含完整的主机名和路径。

DNS 查找会被缓存,因此如果系统配置正确,就不会出现多次查找。

Are relative links much faster than site links?

no, because they're one and the same. Relative links are a merely a webmaster convenience provided by the browser. They, too, are resolved into absolute links in the end: Every request that is sent to the server always contains the full hostname and path.

DNS lookups are cached, so there won't be multiple lookups if the system is configured properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文