PHP 301 and 302 redirect in 2023

In this PHP tutorial I am going to teach you how to perform a redirect, correctly, yes, without messing it up, with PHP and using the famous function headers.

I’m going to leave you how to do the two types of redirect: the 301 (permanent) and the 302 (temporary).

Thanks to these two types of redirects, you will be able to move the users of your web page at will, between one page and another. In addition, you will be able to have control over what happens on your website in the face of search engines.


  • What is a PHP redirect for?
  • Types of redirects
  • PHP 301 Redirect
  • Tips for php redirects

What is a PHP redirect for?

A php redirect or another language, serves to send the user’s browser to a different page.

A redirection can have multiple uses, such as sending the user to a URL with content that the user wants to see or that is more suitable for a certain type of visit.

An example of a web redirect would be a redirect after performing a login successfully on any web page.

The key to this type of reduction, at least depending on the use case, is that is correct from the point of view of use.

If, for example, you are developing for the front end of a web open to the public, normally you will have to comply with the technical SEO of the web, and if you do not properly mark the HTTP header you are “messing” it.

In this way, in a backoffice the type of header code would not matter, in a front office No.

Types of redirects

There are mainly two types of web redirects. At least the most used are:

  • permanent redirect with HTTP code 301. This redirection means that the page being visited no longer exists and has a “new name”, that is, a new slug.
  • temporary redirect with HTTP code 302. This redirection means that the visited page is temporarily not in service and the user is redirected to another page. Usually for some kind of maintenance or for some temporary reason.

It is important to emphasize that the user who visits our page does not realize what redirection we are doing, regardless of the type, but this information is of vital importance for seekers, crawlers and/or browsers.

In addition to what has been said, tell you that thanks to the type of redirection used, search engines like Google can update their databases on the different pages of your website.

Yes, I’m talking about SEO, if you don’t know what it is, nothing happens, at least if you don’t work on a website e-commerce or on a website that must be positioned.

If not, I invite you to learn a little about SEO.

If the term SEO sounds familiar to youKnowing how to use the correct type of redirection for each situation will be decisive for the positioning of your website.

PHP 301 Redirect

To make a redirect in PHP 301 or 302 we will use the header() function. This function allows you to configure the HTTP headers that our server sends.

An example PHP redirect could be the following:

header("Location: htts://"); 

As you will have seen, at no time have I indicated the redirection code, this is possible because this function, defaultconfigure any redirects with the code 302.

Therefore, by default, all redirects that do not specify code will be of type temporary.

If we want to perform a 301 redirect we will use the function as follows:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: nueva_pagina.html"); 

Tips for php redirects

  • Note that before a header call nothing should be sentneither HTML nor plain text, nothing at all.
  • The ideal in many cases after the redirection is end the script with exit php. In this way we avoid possible errors by sending more data in the response.
  • There is also the 308 redirect, yes, I have not mentioned it, but it is a 301 that does not change the method used. That is, a 301 can go from a POST to a GET method, a 308 redirect cannot.
  • Redirects are often used in many cases for the final page of a FORM. 301 from the page where the data is processed to the end, with an “OK” or “Oops, there was an error” message.

Leave a Reply