How does HTTP 302 work? I would like to know the internals
You mean how do browsers handle it? The server sends a 302
code along with a Location
header, and the browser requests the new URI specified by the Location
header instead.
Unlike 301 (Moved Permanently)
, the browser continues to use the original URI to do requests, in case the 302
code goes away
The internals of what? 302 is a return code the server gives the client, what the client does is upto it. The RFCs give guidance on what the client should do, but in the real world 301, 302, 303 and 307 are all handled the same way by the mainstream browsers.
The server returns an HTTP response with the code 302
, indicating a temporary redirection, and includes a Location:
header indicating the new URI, e.g.
HTTP/1.1 302 Found
Location: http://some-other-url
And potentially other headers at the server's discretion.
The browser normally takes this as a directive to automatically make a new, separate request for the other URI specified by the location header. The client (browser) isn't forced to do this (it could, in theory, just display a message to the user, or do whatever else it wants), but that's how HTTP clients usually behave.
Note that since the 302 is a temporary redirection, a well-behaved client will continue to use the old URL in the future, rather than going directly to the new one (301 is a permanent redirection).