Location
Description
The Location
response header indicates the URL to which the client should navigate after receiving the response. This header is primarily used with redirection responses (status codes 3xx) to direct the client to a new URL where the requested resource can be found.
The Location
header is essential for implementing URL redirections, which are useful in various scenarios such as:
- URL shortening services
- Forwarding from old URLs to new ones
- Redirecting after form submissions
- Implementing authentication flows
- Load balancing and traffic distribution
When a client receives a response with a redirection status code and a Location
header, it typically automatically follows the redirect by making a new request to the specified URL.
Syntax
The syntax of the Location
header follows this structure:
<url>
: An absolute or relative URL where the client should navigate.
Example Syntax
This example directs the client to navigate to the absolute URL https://example.com/new-page
.
This example directs the client to navigate to the relative URL /relative-path
on the same origin.
Examples
Temporary Redirect Example
A response with a 302 (Found) status code, indicating a temporary redirect:
HTTP/1.1 302 Found
Date: Mon, 02 Jun 2025 13:00:00 GMT
Location: https://example.com/temporary-page
Content-Type: text/html
Content-Length: 154
<html>
<head><title>Moved Temporarily</title></head>
<body><p>The document has moved <a href="https://example.com/temporary-page">here</a>.</p></body>
</html>
Permanent Redirect Example
A response with a 301 (Moved Permanently) status code, indicating a permanent redirect:
HTTP/1.1 301 Moved Permanently
Date: Mon, 02 Jun 2025 13:10:30 GMT
Location: https://example.com/new-permanent-location
Content-Type: text/html
Content-Length: 162
<html>
<head><title>Moved Permanently</title></head>
<body><p>The document has moved <a href="https://example.com/new-permanent-location">here</a>.</p></body>
</html>
Post-Redirect-Get Pattern Example
A response after form submission, redirecting to a success page:
HTTP/1.1 303 See Other
Date: Mon, 02 Jun 2025 13:20:45 GMT
Location: /form-submitted-successfully
Content-Type: text/html
Content-Length: 124
<html>
<head><title>Redirecting</title></head>
<body><p>Redirecting to <a href="/form-submitted-successfully">result page</a>...</p></body>
</html>
In this example, the 303 (See Other) status code is used to indicate that the client should use a GET request to access the URL specified in the Location header, regardless of which method was used in the original request.
Redirect After Authentication
A response redirecting a user after successful authentication:
HTTP/1.1 302 Found
Date: Mon, 02 Jun 2025 13:30:15 GMT
Location: /dashboard
Set-Cookie: session=abc123; Path=/; HttpOnly; Secure
Content-Type: text/html
Content-Length: 131
<html>
<head><title>Redirecting</title></head>
<body><p>Login successful. Redirecting to <a href="/dashboard">dashboard</a>...</p></body>
</html>
Summary
The Location
response header is a critical component of HTTP redirection mechanisms, telling clients where to go next after receiving a response. When used with appropriate redirection status codes (3xx), this header enables a wide range of important web functionality, from simple URL forwarding to complex user flows. The browser typically handles these redirects automatically, making the process seamless for users. When implementing redirects, it's important to choose the appropriate status code (301, 302, 303, 307, etc.) based on the specific requirements of your application and the intended behavior for clients.