What is the difference between a 301 and a 302 HTTP redirect, and when should a Node.js application use each?
Short answer
A 301 (Moved Permanently) redirect tells the client (and search engines) that a resource has permanently moved to a new URL, and future requests should go directly to the new location, with browsers and search engines typically caching this redirect and updating their own…
Advertisement
AModel answer
A 301 (Moved Permanently) redirect tells the client (and search engines) that a resource has permanently moved to a new URL, and future requests should go directly to the new location, with browsers and search engines typically caching this redirect and updating their own references. A 302 (Found, or historically 'Moved Temporarily') indicates the move is temporary, and the client should continue using the original URL for future requests -- using the wrong status code can cause search engines to incorrectly deindex a URL permanently, or fail to update their index when a change genuinely is permanent.
app.get('/old-path', (req, res) => res.redirect(301, '/new-path')); // permanent, update bookmarks/SEO
app.get('/maintenance-redirect', (req, res) => res.redirect(302, '/status')); // temporaryReal-world example
A company permanently renaming a product's URL structure uses a 301 redirect from the old URLs to the new ones, ensuring search engines transfer the old page's accumulated SEO ranking to the new URL rather than treating it as an entirely new, unranked page.
Likely follow-ups
- What SEO consequence results from mistakenly using a 302 for a change that's actually permanent?
- What's the difference between a 302 and a 307 redirect regarding whether the request method and body are preserved?
Related topics