Languages
[Edit]
EN

HTTP - 301 vs 302 redirection - when to use which one?

5 points
Created by:
daniell
490

In this short article we want to explain when to use 301 or 302 location redirection status with HTTP requests.

Location redirection with 301 status

It should be used when we want to replace permanentally some URL and we want to tell to search engines  that URL is depricated and was replaced by new one.

Note: many browsers cache responces with 301 status, that causes the web browser doesn't request server again opening directly new one link - it can lead to reopen URL from previous request.

Browser request example:

GET /path-to-old-file-name.html HTTP/1.1
Host: www.example.org

Server response example:

HTTP/1.1 301 Moved Permanently
Location: https://www.example.org/path-to-new-file-name.html

Location redirection with 302 status

It should be used when we want to:

  • have many links to same resource and all of them are important too (it is like shortcut of some link that)
  • be sure that web browser requests server always before redirection occures to get most current redirection URL becouse it can be changed by server in anytime.

Browser request example:

GET /index.php HTTP/1.1
Host: fb.com

Server response example: 

HTTP/1.1 302 Found
Location: https://facebook.com/index.php

Advices (Browser Caching of 301 Redirects)

  • Clear your browser cache each and every time you make a change to a 301 redirect,
  • Never put a 301 (permanent) redirect in place unless it is truly permanent!
  • Always implement 302 (temporary) redirects first, then change them to 301 (permanent) redirects once you’ve tested them!

Resources

  1. https://wpscholar.com/blog/browser-caching-301-redirects/
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

HTML

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join