Redirection allows you to redirect the client browser to a different URL. You can use it when you're switching domains, changing how your site is structured, or switching to HTTPS.
In this article, I'll show you how to redirect to another page with PHP. I'll explain exactly how PHP redirects work and show you what happens behind the scenes.
Learn PHP With a Free Online Course
If you want to learn PHP, check out our free online course on PHP fundamentals!
How Does Basic Redirection Work?
Before we dive into the specifics of PHP redirection, let’s quickly understand how exactly HTTP redirection works. Take a look at the following diagram.
Let’s understand what’s going on in the above screenshot:
- The client browser requests a specific page from the server. In the above example, the client has requested the contents of the index.php file.
- The server receives the index.php file request and wants to inform the client that it’s no longer available or moved somewhere else, and it should look to a new file instead: new_index.php. The server sends the
Locationheader with a new URL along with the 301 or 302 HTTP code. These are the HTTP codes for redirection.
- When a client browser encounters the 301 or 302 code, it knows that it has to initiate another request to a new URL to fetch the content. It initiates a request to fetch the
new_index.phpfile in the above example.
- Finally, a server sends the contents of the new URL.
So that’s how a basic HTTP redirection works. In the next section, we’ll discuss how PHP redirection works.
How Redirection Works in PHP
In PHP, when you want to redirect a user from one page to another page, you need to use the
header() function. The header function allows you to send a raw HTTP location header, which performs the actual redirection as we discussed in the previous section.
How to Use Header Function
Let's go through the syntax of the
header( $header, $replace, $http_response_code )
$header: This is the HTTP header string that you want to use. In our case, we’ll use the
Locationheader for redirection.
$replace: It’s an optional parameter which indicates whether the header should replace a previous similar header.
$http_response_code: It allows you to send a specific response code.
Now, let’s have a look at the following example to understand how it all works together.
<?php // index.php header("Location: https://www.yoursite.com/new_index.php"); exit(); ?>
When the above script is executed, it’ll redirect the client browser to http://www.yoursite.com/new_index.php. In the background, it sends a raw HTTP
Location header along with the 302 status code. The 302 status code is used for temporary redirection, but if you want permanent redirection, you can pass the 301 code in the third argument, as shown in the following snippet.
<?php // index.php header("Location: http://www.yoursite.com/new_index.php", TRUE, 301); exit(); ?>
The 301 permanent redirect allows you to inform the search bots that the page is no longer available, and it can be replaced with a new page.
Why Should You Use the
Exit() Function After the Header Redirection?
Users with sharp eyes would have noticed that I’ve used the
exit() function in the above example. In fact, it’s mandatory that you use either the
exit() or the
die() function immediately after the header redirection to stop script execution and avoid any undesired results.
So it’s always recommended practice to use one of these functions after redirection.
The Famous Error: Headers Are Already Sent
If you’re an experienced PHP programmer, I’m sure you’ve come across this famous PHP error at some point in your day-to-day PHP development. For beginners, however, encountering this error is really annoying, since it’s really hard to debug and fix. In most cases, they don’t even have a clue that it’s caused by the header redirection.
The rule of thumb is that when you use the
header() function in your script, you need to make sure that you don’t send any output before it. Otherwise, PHP will complain with the "headers are already sent" error. This can happen even if you've sent a single white space before using the header function.
In this post, we discussed one of the important features of PHP programming: redirection. First, we went through the basics of HTTP redirection, and then I demonstrated how it works in PHP.
The Best PHP Scripts on CodeCanyon
Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost one-time payment, you can purchase one of these high-quality WordPress themes and improve your website experience for you and your visitors.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
- PHP11 Best PHP Event Calendar and Booking Scripts... and 3 Free OptionsMonty Shokeen
- PHP10 Best PHP URL Shortener ScriptsMonty Shokeen
- PHP18 Best Contact Form PHP Scripts for 2020Monty Shokeen
- PHPComparing the 5 Best PHP Form Builders (And 4 Free Scripts)Monty Shokeen
- PHPCreate Beautiful Forms With PHP Form BuilderAshraff Hathibelagal
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post