How to make a redirect in PHP

In this tutorial I will explain how to correctly perform a redirect in php. For this assignment I am going to show you how to do a basic redirect, several redirect examples and also important notes that could ruin your web redirect.

To be exact, the concepts that I am going to deal with are:

  1. How to make a basic redirection in PHP?
  2. How to perform a redirect according to the situation.
  3. Notes and details to take into account for avoid errors when manipulating headers.
  4. Creation of a helper to reuse in projects and avoid forgetting a complete configuration.


  • The easiest way to redirect in PHP
  • How to redirect with a redirect code
    • Example with 301 redirect
    • Example with 302 redirect
  • Helper to perform redirects easily
  • Important notes to make a correct redirection

The easiest way to redirect in PHP

the way to perform redirects in PHP is through the function header(). Let’s see how to perform a simple and fast redirection:

$nuevaURL = '';
header('Location: '. $nuevaURL);

How you can see its use is extremely simple, but there is one detail to take into account important when we manipulate headers in php this command must be executed before any text is printed on the screen, either, for example, through a command threw out or with normal use of HTML.

How to redirect with a redirect code

How well will you know the status codes in headers They are an important mechanism for communication between clients and servers. Thanks to these codes, the questions and answers that occur in tcp/ip connections make sense. Therefore, since we are modifying the response headers of our server, we should use appropriate code for each situation.

In our case, as this is a redirectwe must use a range code 300. Specifically, for our website we should use one of the main redirect codes:

  • 301 redirect code, permanently moved. When a page of our site is no longer going to be available again, we must carry out the redirection and launch this HTTP code. In this way we are notifying the visitor that the first page visited will no longer exist and that the new one is the one that now replaces it.
  • 302 redirect code, temporarily moved. This code will mean that the url originally required is not available for a short period of time.

If you get by with English and you are more interested in header codes, I recommend you visit the w3c documentation: HTTP/1.1 status codes.

Let’s look at an example with both HTTP status codes.

Example with 301 redirect

header("HTTP/1.1 301 Moved Permanently"); 
$nuevaURL = '';
header('Location: '. $nuevaURL);

Example with 302 redirect

By default PHP using the command headers perform a temporary redirectTherefore, to create a redirect with a 302 status code, we just have to not indicate any HTTP code.

$nuevaURL = '';
header('Location: '. $nuevaURL);

Helper to perform redirects easily

Given that redirecting visitors to our websites is a very common task and which is constantly called upon, I recommend creating a reusable function and including it in a file helpers with a similar task.

My proposal is the following, and it is the one I often use in my projects:

function redirect($uri = '/', $codigo = 'default') {
		switch( $codigo ){
			case 301:
				header( 'HTTP/1.1 301 Moved Permanently' );
				header('Location: '.WEB.'/'.$uri);
				header('Location: '.WEB.'/'.$uri);	

To use this function you should have previously created a constant with the URL of the site:

define( 'WEB', '' );

Now, to call the function and launch a redirect you can do it by:

redirect( '/contacto', 301 );
//o para una redirección 302

Important notes to make a correct redirection

To finish, I would like to leave you with all the key points to create redirections correctly and that they do not produce errors:

  1. The function call headers you must do it before printing any content on the screen.
  2. Think about what status code you need for each redirection, although a priori it does not seem very important, if you are creating websites that require good SEO, you could be creating errors that GOOGLE or another SERPs would penalize.
  3. Don’t abuse redirects, since it is a practice punished by search engines if it is used frequently. Also, it reduces the performance of your site.
  4. Make a stop call with the function die always after a redirect, since certain bots either crawlers they could ignore the redirect and access a priori protected content.
  5. A PHP redirect is always preferable to a web redirect. javascript or HTML.

Leave a Reply