PHP Headers: 301 redirect vs 302 redirect

Adding an exit is needed for a 301 redirect to work correctly in PHP
Call exit after using header in PHP to ensure 301 redirect turns it into a 302 redirect.

Many are confused on how and when to use a 301 vs 302 redirect. Additionally, how to set the HTTP status code in PHP can be done easily as you understand how it functions. This tutorial covers all of these topics.

301 VS 302 Redirect

The 301 and 302 HTTP status codes are both used for redirecting from one URL to another. Knowing when to use one vs the other, particularly for proper SEO, is important.

  • 301 Redirect: moved permanently
  • 302 Redirect: moved temporarily

SEO Usage of 301 VS 302 Redirect

It’s important to use the proper redirect HTTP status code to maintain SEO value of pages.

301 Redirect (HTTP Status Code: 301)

When a URL moves and is never intended to be moved back to that URL, a 301 redirect is the status code of choice. This is used when restructuring URLs across the site, etc. When a 301 redirect is used, most of the SEO value is passed from URL A to URL B. I say most because each search engine has it’s own algorithms as to how this value is passed. If a URL needs to be redirected, this is typically the status code of choice.

When a search engine sees a 301 status code from crawling a URL, it is stored on their end that URL A should now be considered as moved to URL B.

302 Redirect (HTTP Status Code: 302)

A 302 redirect is less common. It is used in temporary situations. For example, if a page is being redesigned and for a short amount of time it needs to be served from a different URL while development is done, then use a 302 redirect. Redirect URL A to URL B for a short period of time. Then remove the 302 redirect and URL A once again becomes the permanent URL.

Search engines typically don’t modify SEO values while this is being used. But don’t abuse this status code as it could confuse your intentions.

How to Return a 301 status code Redirect using PHP

Writing a redirect in PHP is done with the header function in PHP. Headers need to be set in PHP before any output is called. This means any calls to echo, print, var_dump, etc. will throw an error.

Redirect headers are written with these simple lines:

These two header lines do two things:

  1. Set the HTTP status code and message. The message should follow the standard HTTP status codes as defined by W3C prefixed by HTTP/1.1 .
  2. The Location header sets the URL that the page is being redirected to.

But, there is one important piece missing here. Once the headers are all set, execution needs to be terminated by calling exit;

With the sample above, the specified 301 redirect would actually return a 302 HTTP status code as opposed to the expected 301.

Here is the complete (simplified) code to ensure a 301 status code is returned.

A quick explanation on this – when the page is loading, if it passes this script at any time, a the status code is added the header and the location is passed.  This tells a search engine that the page has now moved it’s URL and that they should remember this change on their end.

How to Return a 302 status code to Redirect using PHP

With the knowledge of how a 301 redirect is done in PHP, a 302 HTTP status code can also be easily returned. While above it was identified that omitting the exit; call would result in a 302 redirect, do not rely on that as a solution. Instead properly set the headers.

Pro Tip: HTTP Status Code Chrome Extension

In the browser, it’s helpful to quickly identify HTTP status codes that a URL has responded with.

The browsers developer tools report the HTTP status codes, but that has to be opened and takes many clicks to get to the information needed. This makes it slow to check if a 301 or 302 status code was used, if multiple redirects are at play, etc.

There is a Chrome browser extension, Redirect Path, that shows the status code of the pages URL right at the top of the browser without extra clicks to open more tools. This is a plugin highly worth using.