Languages
[Edit]
EN

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

5 points
Created by:
Marcin
2478

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/
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.