Common sense of Redirect

When I got off work today, I saw some basic knowledge of redirection, which is also an eye-opener. I often used 301 and 302 before, but I have never used and understood other 3XX status codes. I found that there are still a lot of knowledge and problems solved in the original.

The redirected process

The browser first visits the URL of server A, server A returns the header with the URL of location B and the status code of 3XX, the browser reads the response 3XX status code, gets the location of the header, and then jumps to server B URL.
You need to know that the jump is initiated by the browser. If the server returns a 3XX status code to a non-browser terminal, the redirection may not be completed.
In a certain year, there was an API interface written in PHP that should have been running for a long time. I have been using HTTP, which is often hijacked, and then the leader wants to replace it with encrypted HTTPS, but the client cannot publish the version. Later, the server side considered changing the interface of the whole site from HTTP 302 to HTTPS to discuss the feasibility of this scheme. If you know the above process and knowledge, this plan will pass immediately.

Permanent redirect

Indicates that the resource is permanently redirected to the new URL.
A more common case is the migration of the old site to the new site. After the old site is directly closed, the page of the old site has been included in the search engine. At this time, the permanent redirection scheme is used.
Permanently redirect the two status codes
301. The redirect request usually uses the GET method, regardless of the method used in the original request.
308. In order to supplement 301. Redirection must use the original request method and package body access.

Temporary redirect

Indicates that the resource is only temporarily redirected to a new URL.
There are five status codes for temporary redirection. Commonly used are two corresponding 302 and 307.
302. The redirection request usually uses the GET method, regardless of the original request. Kind of method.
303. It does not mean that the resource is changed, but only means that the response of the new URL is used to replace the original request. Regardless of the method used in the original request. Basically the same as 302, so 303 is rarely used in the market, and 302.
307 is used . In order to supplement 302. Redirection must use the original request method and package body access.
Baidu is using 307 to jump, browser input http://www.baidu.com will 307 to https://www.baidu.com

300. There are multiple possible responses to this request, and the browser can choose one of them. There is no standard that the server can follow to make choices instead of users.
304. Tell the browser that the requested content has not changed since the last visit. The resource can be obtained directly from the browser cache.
The latter two are not commonly used.

Most used are 301 302 307 308

The problem of circular redirection

ERR_TOO_MANY_REDIRECTS is
a very common error. If you visit page A and then redirect to visit B, and then B makes the redirect visit to A, this is a loop redirect. This error will also be reported after multiple redirects.

I encountered it once in the production environment. Once I found an error of ERR_TOO_MANY_REDIRECTS in the log, but the 3XX jump code was not found in the Nginx configuration, so how did it loop jump. Later I saw this configuration

    location / {
        try_files $uri $uri/ /index.html$is_args$args;
    }

Later, it was found that there was no index.html in the root directory, uri did not exist, and then uri/ directory did not exist, and finally an internal sub-request was initiated to index.html.index.html did not exist, then to location, and redirected repeatedly. Finally, the error ERR_TOO_MANY_REDIRECTS is reported.

Guess you like

Origin blog.csdn.net/feifeixiang2835/article/details/94051474