301 vs 302 Status Code: Key Differences and When to Use Each
301 status code means a permanent redirect, telling browsers and search engines the resource has moved forever. The 302 status code means a temporary redirect, indicating the resource is temporarily at a different URL but may return.Quick Comparison
Here is a quick side-by-side comparison of the 301 and 302 status codes.
| Factor | 301 Moved Permanently | 302 Found (Temporary Redirect) |
|---|---|---|
| Purpose | Permanent redirect | Temporary redirect |
| Browser behavior | Updates bookmarks and caches new URL | Does not update bookmarks or cache permanently |
| SEO impact | Passes link equity to new URL | Does not pass full link equity |
| Use case | Resource moved permanently | Resource temporarily moved or under maintenance |
| Client caching | Clients cache the redirect | Clients usually do not cache the redirect |
| HTTP method | Usually preserves original HTTP method | May change POST to GET in some clients |
Key Differences
The 301 status code signals that the requested resource has been permanently moved to a new URL. This means browsers and search engines should update their records to use the new URL in the future. It is commonly used when a website changes its domain or reorganizes its pages permanently.
On the other hand, the 302 status code indicates a temporary redirect. The original URL is still valid and may be used again later. Browsers typically do not update bookmarks or cache the new URL permanently. This is useful when a resource is temporarily moved, such as during maintenance or A/B testing.
From an SEO perspective, 301 redirects pass most of the original page's ranking power to the new URL, while 302 redirects do not pass full link equity because the move is not permanent.
Code Comparison
Here is an example of how to send a 301 Moved Permanently redirect in a simple Node.js Express server.
const express = require('express'); const app = express(); app.get('/old-page', (req, res) => { res.redirect(301, '/new-page'); }); app.get('/new-page', (req, res) => { res.send('This is the new page.'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
302 Equivalent
Here is how to send a 302 Found temporary redirect using the same Node.js Express server.
const express = require('express'); const app = express(); app.get('/temp-page', (req, res) => { res.redirect(302, '/new-temp-page'); }); app.get('/new-temp-page', (req, res) => { res.send('This is the temporary new page.'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
When to Use Which
Choose 301 when you want to permanently move a resource and inform browsers and search engines to update their links and caches. This is ideal for permanent URL changes, domain migrations, or page reorganizations.
Choose 302 when the move is temporary, such as during maintenance, testing, or temporary promotions. This keeps the original URL valid and prevents search engines from updating their indexes prematurely.
Key Takeaways
301 for permanent URL changes to update browsers and SEO rankings.302 for temporary redirects without affecting SEO or bookmarks.