I was curious how HTTP redirects work and was wondering whether the client has to request once or twice.
When the client requests the url http://example.com/foo
and the server response with a 301
(permanent redirect) to http://example.com/bar
, does the server also send the response of the /bar
url? Or does the client has to request /bar
?
I've tried to express my thought in graphics:
1:
deceze's answer is completely correct for HTTP/1.1 and earlier, i.e. the server must wait for the client to process the 301 and request the new URL, or defer to its cache.
In HTTP/2, the server can send both the 301 for /bar
and the 200 for /bar.html
at (more or less) the same time, using a feature called Server Push. Server Push is described in RFC 7540, section 8.2. Basically, while the server is sending the 301 for /bar
, it sends another stream with the 200 for /bar.html
, along with information on what the client request for /bar.html
would have looked like (the method, resource, HTTP headers in the request, etc.).
Server Push does require that both client and server support HTTP/2, that they both support Server Push, and that the client has not requested that Server Push be disabled by setting the SETTINGS_PUSH_ENABLED parameter to 0. Any proxies or other intermediaries between the server and client are also free to choose not to pass on the server's pushed content to the client.
So if your server supports HTTP/2, graphic #1 is possible, but you cannot guarantee that it will happen, though most modern browsers do support HTTP/2 with Server Push.
The server can only send one response, it cannot answer with a 301 and a 200. The client has to make another request for the new resource. This also allows the client to make use of their cache, perhaps there's no actual need for the client to request the resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With