Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP: When redirecting, does the server send response immediately?

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:

Option 1 (only 1 request)


2:

Option 2 (2 requests)

like image 204
Jonas Drotleff Avatar asked Oct 15 '25 18:10

Jonas Drotleff


2 Answers

HTTP/1.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.

HTTP/2: Server Push

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.

like image 165
Jason Hoetger Avatar answered Oct 19 '25 05:10

Jason Hoetger


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.

like image 27
deceze Avatar answered Oct 19 '25 05:10

deceze