PHP powers some of the most popular websites in the world, including WordPress websites. It’s a popular scripting language for website development because it’s fast, flexible, and easy to learn. PHP is primarily used to create dynamically generated web pages fast — although it can do much more.
For example, PHP can simplify certain tasks like setting up a redirect. Let’s take a closer look at what a PHP redirect is, then walk through how to set one up.
What is a PHP redirect?
A PHP redirect is a server-side solution to forwarding users and search engines from one URL to another using the header() function. Since its server-side — as opposed to an HTML redirect, which is client-side — a PHP redirect provides faster and more secure navigation from one page to another.
Now that we understand the benefit of a PHP redirect, let’s look at how to set one up.
How to Redirect in PHP
To redirect in PHP, you’ll first need to write your header() function, starting with header().
Then, within the parentheses, you’ll define the Location response-header field with the URL or file name you wish to redirect users and search engines to. Supported files include PHP, HTML, Python, CGI, Perl, or compiled CGI programs. So your header function might look something like this: header("Location: http://www.example.com/"); or header("Location: example.php/");
After the semicolon, you’ll have to add one last function: either the die() or exit() function. Without either of these functions, search engine crawlers or bots can ignore the header function and continue processing the page you wanted to redirect away from. Here’s how your header might look now: header("Location: http://www.example.com/"); exit;
Finally, you’ll wrap this function in <?php and ?> tags. The final result will look something like this:
PHP Redirect Header
There are some additional rules for using the header() function to set up a PHP redirect. First, where you place the header() function in your index.php file matters. Second, you can set HTTP status codes to control how the server redirects a user and search engine.
Let’s take a closer look at these rules below.
PHP Header Location
For the PHP redirect to work, the header() function must execute before any output is sent. That means, the code must be written above the <!DOCTYPE html> or <html> tags in your index.php file.
Otherwise, you'll likely get an error message that says "headers are already sent." Supposedly, the header function is so finicky that a single white space can prompt this error.
Here’s a look at the proper location:
Here's a look at an improper location:
HTTP Status Response Codes
HTTP status response codes let you know whether or not your HTTP request was successfully completed. There are different groups of response codes, including redirects. Since a redirect status code changes the way browsers and search engine bots handle redirects, it’s recommended that you set a status code when using header(Location:). Let’s take a look at the differences between the two most common redirect status codes below.
If the status code is not specified in the header function, then it defaults to 302. 302 indicates a temporary redirection. Meaning, the requested URL resides temporarily under a different URI.
With a 302 redirect, browsers will typically cache the page for the session and no longer. Search engines will not typically transfer page rank to the new location either. That makes the 302 redirect ideal for performing site maintenance or other temporary use cases.
Here’s an example of the header call with the 302 code specified:
This call not only sends the header back to the browser — it also returns a 302 redirect status code.
PHP 301 Redirect
If you would like to set the PHP redirect to be permanent instead of temporary, then you can use the status code 301. 301 indicates a permanent redirection so the browser will automatically redirect a user using the old URL to the new address of the page, and inform search engine bots that the page is no longer available and can be replaced with the new page. As a result, a 301 redirect is considered the most user and search engine-friendly 3xx code.
The other key differences between 302 and 201 redirects are that browsers will typically cache the page for longer than the session, possibly even indefinitely, and search engines typically transfer page rank to the new location. That makes 301 redirects ideal for redirecting duplicate content, migrating to a new domain, and more.
Here’s an example of the header call with the 301 code specified:
Setting Up Your PHP Redirects
PHP redirects can help users and search engines navigate smoothly and securely between pages on your site. The good news is setting up these redirects is easy thanks to the header() function.
Originally published Dec 7, 2020 7:00:00 AM, updated November 02 2021