'two requests to the server' 在这种情况下的实际含义是什么?

What's the actual meaning of 'two requests to the server' in this context?

在 w3schools.com 上 HTML Links Chapter 学习 HTML 教程时,我看到了以下句子:

Without a forward slash on subfolder addresses, you might generate two requests to the server. Many servers will automatically add a forward slash to the address, and then create a new request.

锚标签的 href 属性中提到的 URL 如果尾部斜杠丢失,我没有理解如何传递两个请求的意思?

假设您有一个 URL 这样的:

http://example.com/myfolder/

因为在许多服务器中,directory 可以在没有尾部斜杠的情况下访问,但是有一个很好,对吧?所以,服务器,一般重定向客户端把斜线放在最后。

为什么有两个请求?

看下面客户端和服务器的对话。

  1. 客户请求 #1: 我请求你 http://example.com/myfolder.
  2. 服务器响应 #1:我希望你转到 http://example.com/myfolder/HTTP 302 Found - 这是临时重定向。
  3. 客户请求#2:啊,好的!我请求你 http://example.com/myfolder/.
  4. 服务器响应 #2:好的,给你。 HTTP 200 OK - 这是您的内容。

所以客户端向服务器发出了两次请求。希望你明白。

ps:请不要使用W3Schools,有更多更好的资源。

接收到 /foo 请求的 Web 服务器可能会查看它并发现它的 foo 实际上是一个文件夹,并决定 actual此文件夹的地址是 /foo/。因此它将以 302 重定向响应到客户端并告诉它获取 /foo/ 而不是:

  1. 浏览器到服务器:GET /foo
  2. 服务器到浏览器:302 Found, Location: /foo/
  3. 浏览器到服务器:GET /foo/
  4. 服务器到浏览器:200 OK, content...

服务器是否执行此操作取决于您的服务器。最后,您应该始终在链接中使用 实际 URL 项目,不要依赖服务器为您隐式 "fix it"。