Summary
HTTP redirection is a mechanism that instructs a client (typically a web browser) to automatically request a different URL than the one originally requested. It is a fundamental part of the HTTP protocol, used for tasks such as moving content, handling URL normalization, load balancing, and enforcing secure connections.
Key Points
- Redirection is signaled by HTTP status codes in the 3xx range, most commonly 301 (Moved Permanently) and 302 (Found).
- The target URL is provided in the
Locationheader of the server's response. - Clients follow redirections automatically, often with a limit on the number of hops to prevent infinite loops.
- Redirection can be transparent to the user but may affect SEO, latency, and caching behavior.
- Different status codes indicate different semantics (permanent vs. temporary, cacheability, method preservation).
Concepts
- 301 Moved Permanently: Indicates the resource has a new permanent URL. Browsers and search engines update their records. The request method is usually changed to GET (for POST requests).
- 302 Found (or 307 Temporary Redirect): Indicates a temporary move. Clients should continue using the original URL for future requests.
307preserves the original HTTP method;302may change it to GET. - 303 See Other: Forces a GET request to the new URL, often used after form submission to avoid duplicate submissions.
- 308 Permanent Redirect: Like 301 but preserves the HTTP method (e.g., POST stays POST).
Locationheader: The response header that contains the target URL for redirection.- Redirect chain: A series of redirections (e.g., A → B → C). Too long chains degrade performance.
- SEO impact: 301 redirects transfer link equity; 302 does not. Improper redirects can cause ranking loss.
Details
HTTP redirection works via a request–response exchange:
- The client makes an HTTP request to a URL.
- The server responds with a 3xx status code and a
Locationheader pointing to the new URL. - The client (browser or HTTP library) automatically issues a new request to the URL in the
Locationheader. - The process may repeat if the new URL also returns a redirection.
Common use cases include:
- URL migration: When a website changes its domain or URL structure, 301 redirects preserve bookmarks and search rankings.
- Protocol enforcement: Redirecting
http://tohttps://(often with a 301 or 302). - Route canonicalization: Redirecting
example.comtowww.example.comor vice versa. - Temporary maintenance: Using 302 to direct users to a maintenance page.
- Load balancing: Some load balancers return a 302 to direct clients to a specific backend.
- A/B testing: Redirecting a percentage of traffic to an alternative version.
Best practices:
- Use permanent (301) redirects only when truly permanent, as browsers cache them aggressively.
- Avoid redirect chains (more than 2–3 hops) to minimize latency.
- Ensure the
LocationURL is absolute and properly encoded. - For API clients, respect the method‑preserving codes (307, 308) to avoid breaking POST/PUT semantics.
Rendering diagram…
The diagram shows a simple permanent redirect: the client follows the Location header and retrieves the new resource.