0
0
Rest-apiComparisonBeginner · 4 min read

301 vs 302 Status Code: Key Differences and When to Use Each

The 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.

Factor301 Moved Permanently302 Found (Temporary Redirect)
PurposePermanent redirectTemporary redirect
Browser behaviorUpdates bookmarks and caches new URLDoes not update bookmarks or cache permanently
SEO impactPasses link equity to new URLDoes not pass full link equity
Use caseResource moved permanentlyResource temporarily moved or under maintenance
Client cachingClients cache the redirectClients usually do not cache the redirect
HTTP methodUsually preserves original HTTP methodMay 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.

javascript
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');
});
Output
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.

javascript
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');
});
Output
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

Use 301 for permanent URL changes to update browsers and SEO rankings.
Use 302 for temporary redirects without affecting SEO or bookmarks.
301 redirects pass link equity; 302 redirects usually do not.
Browsers cache 301 redirects but treat 302 redirects as temporary.
Choose the status code based on whether the redirect is permanent or temporary.