What's the actual meaning of 'two requests

2019-02-07 19:10发布

问题:

While studying HTML tutorial on HTML Links Chapter on w3schools.com I saw following sentence:

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.

I didn't get the meaning of how two request can be passed if trailing slash is missing in the URL mentioned in href attribute of anchor tag?

回答1:

A web server receiving a request for /foo may look at it and see that its foo is actually a folder, and decide that the actual address for this folder is /foo/. So it will respond with a 302 redirect to the client and tell it to fetch /foo/ instead:

  1. browser to server: GET /foo
  2. server to browser: 302 Found, Location: /foo/
  3. browser to server: GET /foo/
  4. server to browser: 200 OK, content...

Whether the server does this or not depends on your server. Ultimately you should always use the actual URL of items in your links, don't rely on a server to "fix it" implicitly for you.



回答2:

Consider you have a URL like this:

http://example.com/myfolder/

Since in many servers, the directory can be accessed without a trailing slash, but it is good to have one, right? So, the server, generally redirects the client to put the slash in the end.

Why two requests?

See the below conversation between the client and server.

  1. Client Request #1: I am requesting you http://example.com/myfolder.
  2. Server Response #1: I want you to go to http://example.com/myfolder/, HTTP 302 Found - This is a temporary redirect.
  3. Client Request #2: Ah, OKay! I am requesting you http://example.com/myfolder/.
  4. Server Response #2: Sure, here you go. HTTP 200 OK - Here's your content.

So there are two requests from the client to the server. Hope you understand.

ps: Please do not use W3Schools, there are lot more better resources.