HTTP redirects

301 vs 302 vs 307 vs 308: Choosing the Right Website Redirect

A website redirect tells a browser that the requested URL should lead somewhere else. That sounds simple, but the HTTP status code changes what the move means and how a client handles the next request. Choosing the wrong code can produce broken forms, confusing caches or a migration that never quite settles. Here is a practical guide to 301, 302, 307 and 308 redirects.

Start With Permanent Versus Temporary

Use a permanent redirect when the old URL has been replaced for the foreseeable future. A 301 or 308 communicates that the resource has a new permanent address. Typical examples include moving a page to a new path or consolidating a site’s preferred hostname. A temporary redirect, usually 302 or 307, fits a short-lived change where the original URL is expected to work again.

Do not use a permanent code for a maintenance detour merely because it seems convenient. Clients can remember permanent redirects, making a later reversal harder to test. During a website migration, plan the final URL map before enabling permanent redirects.

Know What Happens to the Request Method

The important distinction is not only duration. For historical compatibility, a user agent may change a POST request into a GET while following a 301 or 302. Codes 307 and 308 explicitly preserve the original method and request body. That matters for forms, API endpoints and uploads, where silently changing the method can alter the operation or lose submitted data.

For ordinary page navigation, a 301 is a familiar permanent choice and a 302 is a familiar temporary one. When preserving the method is essential, choose 308 for a permanent move or 307 for a temporary one, then test the actual client. A 303 has a different purpose: it explicitly directs the client to retrieve another resource with GET, such as a confirmation page after a form submission.

Build a Clean Redirect Map

Point each old URL directly to its final destination. Avoid a chain that hops through several addresses, and avoid loops that send a browser back to an earlier URL. Preserve a meaningful destination for each page rather than redirecting everything to the homepage. Update internal links and canonical references to the final URLs so visitors do not need a redirect on every click.

Redirects are HTTP responses, not DNS records. Changing hosting or a domain’s DNS settings does not automatically create the page-level redirects your old URLs need. Our overview of HTTP status codes explains where redirects fit among other responses.

Test Both Headers and Behaviour

Check a sample of old URLs, including paths with query strings and any endpoints that accept POST. Inspect the status code and Location header, then follow the redirect to confirm the final page works. A command-line request can expose unexpected chains; our curl testing guide shows how to inspect responses. Also test in a browser, especially after changing a permanent rule that may already be cached.

Document why each redirect exists and remove temporary rules when their purpose ends. For the exact semantics of the status codes, see the redirection section of RFC 9110: HTTP Semantics.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *