Basic concepts: HTTP protocol, response code, redirect
HTTP, or Hypertext Transfer Protocol (Hypertext Transfer Protocol), is the main application layer protocol through which everyone is familiar with surfing the Web using a browser. A characteristic feature of this protocol is the lack of data on the previous state between different client requests made with its help. For each request of a client application (a particular case of which is a browser), the server to which it was addressed generates a response. At the beginning of each response there is one of the predefined codes, or statuses, of the HTTP response, then headers and the response body may be present.For the purposes of this article, only response codes for redirects will be considered. So, a group of codes from 300 to 399 is allocated for redirects. In the previous version of the HTTP protocol specification, the maximum number of redirects was 5, so when using redirects, you need to keep this limitation in mind, which can still be implemented in legacy applications.The very concept of redirection assumes that upon receipt of the 3xx response code, the client application, in particular the browser, independently or with the help of the user, makes a decision about what to do next.
Types of redirects Currently, there are 9 types of redirects:300 Multiple Choices
301 Moved Permanently
303 See Other
304 Not Modified
305 Use Proxy
307 Temporary Redirect
308 Permanent Redirect
Next, we will consider in detail all these types and cases when they need to be used.
Multiple Choices, HTTP Code 300 It is used when the same resource on the Web has several views, each of which has its own location, for different clients. For example, for users visiting the site from a stationary PC or laptop, the regular version of the site will be shown, and for users of mobile devices, a light version of the site will be displayed, which takes into account the peculiarities of the hardware of these devices. The example is somewhat contrived, since to implement this behavior, previously, other data transfer protocols were simply used (for example, WML), and now adaptive layout is used.
Moved Permanently, HTTP Code 301 This is the most common type of redirect that every webmaster faces. It informs the client application that the requested resource has been moved and is now located at a different address. Why is this redirection important? Let’s look at some examples of when you can’t do without a 301 redirect.The publication on the site is moved to another section or renamed, and its address is automatically changed by the site management program. In this case, the use of 301 redirects will allow you to update the search engine index and not lose the positions won through search engine optimization.
The site is transferred to a new domain. In this case, as long as there is control over the old domain, you need to set a redirect to the new domain on it so as not to lose positions in search engines and visitors / customers who remember the address (for example, in their bookmarks) and follow a direct link. …
Merging sites, reservation for a site with a similar spelling domain or domain name in another domain zone. Reservation can be useful if we want to protect ourselves from possible competing sites with a consonant name. When merging sites, do not abandon the domain that has become unnecessary, because these savings can lead to large losses of traffic or make it easier to enter the same niche of a competitor using your old domain name, which has managed to gain some weight for search engines due to its age.
Found, Found, HTTP Code 302 Reports that the requested resource is temporarily located at a different address and can be transferred to the client. Used in the work of modern web servers, link shortening services (URL shortening).
See other sources, See Other, HTTP Code 303 Introduced in HTTP 1.1 protocol version. Much like code 302. Used for more correct redirection if the client application uses the POST method for the request (for example, when submitting forms in a browser). In this case, the proposed resource address instead of the requested one is temporary and should not be remembered by the calling application.
Not Modified, HTTP Code 304 Tells the client that the requested resource has not been modified since the time specified in the custom If-Modified-Since or If-Match request header. It is actively used in browsers when caching of site content is enabled on the server. If the requested page or other resource (picture, script, page styles) has not been changed, then the browser, upon receiving the 304 response code, does not download from the server, but relies on its local cache. This saves client and server bandwidth and results in faster content downloads. For example, with properly configured caching, a site page that we have recently visited will load an order of magnitude faster than one that we have not yet looked at.
Use Proxy, Use Proxy, HTTP Code 305 Introduced in HTTP 1.1 protocol version. Instructs to use a proxy server (intermediate server) to access the resource, the data of which is transmitted in the Location response header. In this case, the user is not required to do anything, since all the work should be done automatically by the client program.
Switch Proxy, Switch Proxy, HTTP Code 306 This code was used in a previous version of the protocol specification. At the moment, the 306 code does not carry any semantic meaning. However, it remains reserved.
Temporary Redirect HTTP Code 307 Introduced in HTTP 1.1 with code 303 to resolve collisions with temporary redirection, which existed in the previous version of HTTP with only code 302. On redirection, the request method cannot be changed from POST to GET.
Permanent Redirect, HTTP Code 308 While in the draft stage. This is similar to code 301, except that the request method cannot be changed from POST to GET during redirection. Using the 308 code improves the redirection of requests from HTML forms using the POST method to resources that have changed their address on a permanent basis. It complements the 301, 302, 307 set of codes for performing permanent and temporary redirects.
As a conclusion When working on a website, a webmaster should have a good understanding of when and how to set up a 301 redirect. Other significant response statuses are used exclusively by web programmers or by programmers who work on server software. Codes from 309 to 399 are not currently used, but may be used in the future.