Let's have a look at how redirects work in HTTP. If you make a request to hexlet.io, you'll get a redirect in response:
telnet hexlet.io 80
GET / HTTP/1.1
host: hexlet.io
HTTP/1.1 301 Moved Permanently
Date: Thu, 14 May 2020 15:00:05 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Thu, 14 May 2020 16:00:05 GMT
Location: https://hexlet.io/
Server: cloudflare
CF-RAY: 59357c73fadaf2a0-WAW
cf-request-id: 02b54c363b0000f2a07c8f0200000001
0
It works this way because hexlet.io only works over HTTPS. But it handles HTTP too. After all, the browser that accesses the site should be prompted to make a redirect.
You may be wondering how it works. The header 300 (redirect format) is returned. In our example, we see 301 Moved Permanently
. It means that we have sent our request to the resource which was permanently moved, and there's no point in ever visiting it again.
This is actively used in SEO. Search engines retain all the parameters of pages at the old address in the index.
Redirects come in many forms, such as the 302 Found temporary redirect
. It's used if something happens, like, if the site doesn't work, or it's blocked, etc. These are mostly technical things.
But how do you know where the redirect is going? Once again, there's no magic – everything is based on the headers.
In general, not even the body matters. The body may or may not show up depending on how the system that the request is sent through works. The main thing is that the Location header appears. It appears if this response status occurs, which indicates where to go.
That's it. Next, the browser would automatically do the redirect. But since we're in telnet, it doesn't do any work for us and returns this redirect.
Sometimes, websites have errors and constantly give out redirects. The browser will try to navigate through them endlessly. There are various protections in place to stop the system from being killed. For example, they know how to track cyclic redirects and tell the system that it should stop.
The libraries that programmers often work with usually have options that tell you whether to follow the redirect and if so, to what depth.
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.