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:
- How to make a basic redirection in PHP?
- How to perform a redirect according to the situation.
- Notes and details to take into account for avoid errors when manipulating headers.
- Creation of a helper to reuse in projects and avoid forgetting a complete configuration.
Content
- 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:
<?php $nuevaURL = 'https://www.srcodigofuente.com'; 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
<?php header("HTTP/1.1 301 Moved Permanently"); $nuevaURL = 'https://www.srcodigofuente.com'; 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 = 'https://www.srcodigofuente.com'; 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:
<?php function redirect($uri = '/', $codigo = 'default') { switch( $codigo ){ case 301: header( 'HTTP/1.1 301 Moved Permanently' ); header('Location: '.WEB.'/'.$uri); default: header('Location: '.WEB.'/'.$uri); } die(); } ?>
To use this function you should have previously created a constant with the URL of the site:
<?php define( 'WEB', 'https://www.srcodigofuente.com' ); ?>
Now, to call the function and launch a redirect you can do it by:
<?php redirect( '/contacto', 301 ); //o para una redirección 302 redirect('/nota-legal'); ?>
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:
- The function call headers you must do it before printing any content on the screen.
- 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.
- 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.
- 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.
- A PHP redirect is always preferable to a web redirect. javascript or HTML.