HTTP Lecture 16 - HTTP Redirection and Jump

background

"Hypertext" contains "hyperlinks", which can jump from one "hypertext" to another "hypertext", which is a fundamental change to the traditional document of linear structure.
The ability to use "hyperlinks" to jump anywhere on the web is also a key feature of the World Wide Web. It connects documents scattered all over the world to form a complex network structure, and users can click links and convert pages at will while viewing. In addition, the browser provides auxiliary functions such as "forward", "backward", and "bookmark", which make it more convenient for users to jump between documents, and have more initiative and interactivity.

The jump action is initiated by the browser user, which can be called "active jump". Passive jump", which has a special term in the HTTP protocol, called "redirection" (Redirection).

redirection process

Accessing the URI "http://www.chrono.com/18-1" with Chrome, it immediately redirects to "/index.html" with a 302.
insert image description here
From this experiment, we can see that this "redirection" actually sent two HTTP requests, the first request returned 302, and then the second request was redirected to "/index.html". But if you don't use the developer tools, you can't see the jump process at all, that is to say, the redirection is "user-insensitive".
The response message returned by the first request: a new header field "Location: /index.html" appears here, which is the secret of the 301/302 redirection jump.
The "Location" field belongs to the response field and must appear in the response message. But it only makes sense with the 301/302 status code, which marks the URI that the server requires to redirect, here is to ask the browser to jump to "index.html".
When the browser receives the 301/302 message, it will check whether there is "Location" in the response header. If there is, the URI is extracted from the field value and a new HTTP request is issued, which is equivalent to automatically clicking the link for us.
The URI in "Location" can use both absolute URI and relative URI. The so-called "absolute URI" is the complete form of URI, including scheme, host:port, path, etc. The so-called "relative URI" means that the scheme and host:port are omitted, and only the path and query parts are incomplete, but they can be calculated from the request context.

You can safely use relative URIs when redirecting if you are only redirecting within the site. But if you want to jump outside the station,
you must use an absolute URI.
If you want to jump to the Nginx official website, you must write "http://" before "nginx.org", otherwise the browser will understand it according to the relative URI, and you will get a URI that does not exist

redirect status code

The most common redirect status codes are 301 and 302, and there are several less common ones, such as 303, 307, 308, etc. They all have the same final effect, allowing the browser to jump to the new URI, but there are some subtle differences in semantics, so pay special attention when using them.
301 is commonly known as "Moved Permanently", which means that the original URI no longer exists "permanently"
, and all future requests must use the new URI.
When the browser sees 301, it knows that the original URI is "obsolete", and it will do appropriate optimization. For example, history records, update bookmarks, next time you may directly use the new URI to access, saving the cost of jumping again. When the search engine crawler sees 301, it will also update the index library and no longer use the old URI.
302 is commonly known as "Moved Temporarily", which means that the original URI is in the "temporarily maintained" state
, and the new URI is a "temporarily worker" that acts as a "top package".
When the browser or crawler sees 302, it will think that the original URI is still valid, but temporarily unavailable, so it will only perform a simple jump page, no new URI will be recorded, and there will be no other redundant actions. The next visit will still be Use the original URI.
301/302 are the most commonly used redirect status codes, and there are a few remaining in 3××:
303 See Other: similar to 302, but requires the redirected request to be changed to the GET method, visit a result page, and avoid POST/PUT repeated operations;
307 Temporary Redirect: similar to 302, but the method and entity in the request after redirection are not allowed to change, the meaning is more clear than 302;
308 Permanent Redirect: similar to 307, the request after redirection is not allowed to change, but It is the meaning of 301 "Permanent Redirect
".
However, the acceptance of these three status codes is low, and some browsers and servers may not support them. You should be cautious when developing them, and use them only after testing to confirm the actual effect of the browser.

Application Scenarios for Redirection

Let's first look at when you need to redirect.
One of the most common reasons is "resource unavailable" and a new URI needs to be replaced.
There are many reasons why it is not available. For example, domain name changes, server changes, website revisions, and system maintenance will all cause the resources pointed to by the original URI to be inaccessible. In order to avoid 404, it is necessary to use redirection to jump to the new URI and continue to provide services for netizens.
Another reason is to "avoid duplication", allowing multiple URLs to jump to one URI, increasing the number of access entries without adding additional workload.
For example, some websites will apply for multiple domain names with similar names, and then redirect them to the main website.
After deciding to implement redirection, the next thing to consider is the issue of "permanent" and "temporary", that is, choose 301 or
302.
301 means "permanent".
If the domain name, server, and website structure have undergone significant changes, such as enabling a new domain name, switching the server to a new computer room, and reorganizing the website directory hierarchy, these are considered "permanent" changes. The original URI can no longer be used, and 301 "permanent redirection" must be used to notify browsers and search engines to update to the new address, which is also one of the factors to be considered in search engine optimization (SEO).
302 means "temporary".
The original URI will return to normal at some point in the future. A common application scenario is system maintenance, redirecting the website to a notification page, telling users to visit again later. Another usage is "service downgrade". For example, during the Double Eleven promotion, the entry of unimportant functions such as order inquiry and point collection is temporarily closed to ensure that core services can operate normally.

Questions about redirection

There are many uses of redirection. If you master redirection, you can gain more flexibility when setting up a website. However, you need to pay attention to two problems when using it.
The first problem is "performance loss ". Obviously, the redirection mechanism determines that there will be two requests-responses for one redirection, which is one more than normal visits.
Although the 301/302 packets are small, the impact of a large number of redirects on the server cannot be ignored. Fortunately, on-site redirection can be reused for long-term connections, while off-site redirection requires two connections. If the quality of the network connection is poor, the cost will be much higher, which will seriously affect the user experience.
So redirection should be used in moderation and must not be abused.
The second problem is "loop jump ". If the redirection policy setting is not considered, an infinite loop of "A=>B=>C=>A" may appear, and the link keeps turning in circles, and the consequences can be imagined.
Therefore, the HTTP protocol specifically stipulates that the browser must have the ability to detect "loop jumps", and when this situation is found, it should stop sending requests and give an error prompt.
The URI "/18-2" of the experimental environment simulates such a "circular jump", which jumps to "/18-1", and uses
the parameter "dst=/18-2" to jump back to itself, realizing An infinite loop of two URIs was created.
Using Chrome to access this address will get the result of "this page is not working properly":
insert image description here

summary

1. Redirection is a jump initiated by the server, which requires the client to resend the request with a new URI, usually automatically, and the user is unaware; 2.301
/302 are the most commonly used redirection status codes, which are "permanent Redirect" and "temporary redirect";
3. The response header field Location indicates the URI to be redirected, which can be in absolute or relative form;
4. Redirection can point one URI to another URI, or multiple URI points to the same URI, which has many uses;
5. When using redirection, you need to be careful of performance loss and avoid loop jumps.

PS: This article is a note after watching Geek.

Guess you like

Origin blog.csdn.net/Elon15/article/details/130736417