PHP powers some of the most popular websites in the world, including those built on WordPress. If your website harnesses the power of PHP, it’s valuable to learn to make the most of it — and one way to do that is with a PHP redirect. Figuring out how to create a PHP redirect will bring new functionality to your site.
We’re going to share everything you need to know about the world of PHP redirects. To begin, we’ll provide a refresher on what PHP is, what a redirect is, and finally, how to redirect in PHP.
- What is PHP?
- What is a PHP redirect?
- How do you create a PHP redirect?
- What are HTTP status response codes?
- Can you set up a PHP redirect without the header function?
- Things to keep in mind when setting up PHP redirects
What is PHP?
The term ‘PHP’ is an acronym for ‘>PHP: Hypertext Preprocessor.’ This open-source scripting language is server-side, and it is typically used for website development. It can be used to create applications, websites, and more.
This popular scripting language gained momentum because it’s flexible, fast, and easy to learn. And while PHP is primarily used to craft dynamically generated web pages quickly, it can do much more than that. For instance, PHP can simplify certain tasks such as setting up a redirect.
Let’s take a closer look at what a PHP redirect is and walk through how to set one up yourself.
What is a PHP redirect?
When you hear the term ‘PHP redirect,’ we’re really discussing a server-side solution to forwarding users and search engines from one URL to a different one. This is achieved by using the header() function.
The difference between an HTML redirect and a PHP redirect is that HTML is client-side whereas PHP is server-facing. Because of this key distinction, a PHP redirect will provide quicker and more secure navigation from one page to another.
However, you should keep in mind that, while there are various benefits associated with implementing a PHP redirect, they could be dangerous if they’re done incorrectly. Therefore, it’s vital that you follow the proper steps to set one up.
Now that we understand the benefit of a PHP redirect, let’s look at how to set one up.
How do you create a PHP redirect?
To create a PHP redirect, you first need to write your header() function. This begins with header().
Next, define the Location response-header field with the URL or file name where you want to redirect users and search engines. Place that within the parentheses. Keep in mind that supported files include PHP, HTML, Python, CGI, Perl, or compiled CGI programs.
For instance, your header function in your PHP redirect may look something like this: header("Location: http://www.example.com/"); or header("Location: example.php/");
Following the semicolon, you have to add another function: either the die() or exit() function. Without either of these functions, search engine crawlers or bots might ignore the header function and continue processing the page you wanted to redirect away from.
This could be problematic because if you’re looking to protect a certain page with a header redirect, forgetting to use die or exit means your page is left vulnerable. Furthermore, this renders the PHP redirect inefficient. Here’s how your header might look after following this step: header("Location: http://www.example.com/"); exit;
Lastly, you have to wrap the function in <?php and ?> tags. The result will look something like this:
PHP Redirect Header
Now that you understand the basic formula for creating a PHP redirect, let’s discuss some additional rules necessary to properly use the header() function.
First, remember where you place the header() function in your index.php file isn’t arbitrary. Second, recall that you may choose to set HTTP status codes. These will control how the server redirects a search engine or user.
PHP Header Location
In order for your PHP redirect to be successful, the header() function must execute — and before any output is sent. This is why your code must appear above the <!DOCTYPE html> or <html> tags in your index.php file.
If you fail to complete this, you’ll probably see an error message that tells you “headers are already sent.” The error might not be a massive one — 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:
What are HTTP status response codes?
There’s one reason why an HTTP status response code appears: to inform you whether or not your HTTP request was successfully completed. However, not all response codes are alike — there are different groups, including redirects.
These are grouped into five different categories: informational (100-199), successful (200-299), redirection (300-399), client error (400-499), and finally, server error (500-599). Because a redirect status code alters how browsers and search engine bots handle redirects, it’s recommended that you set a status code when using header (Location:). Here are two common redirect status codes you should be aware of.
In situations where the status code is not specified in the header function, it will default to 302. Consequently, when you see 302, this indicates a temporary redirection and that the request URL is temporarily residing under a different URI.
If you experience a 302 redirect, your browser typically caches the page for the session only. Additionally, search engines do not typically transfer page rank to the new location. This is why the 302 redirect is ideal for performing site maintenance or other temporary use cases.
Here’s what a header call looks like with the 302 code specified:
header("Location: https://www.examplecom/", true, 302);
This call not only sends the header back to the browser — it also returns a 302 redirect status code.
PHP 301 Redirect
To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address. It also informs search engines that the page isn’t available anymore but is replaced with the new one. Consequently, a 301 redirect is considered the most user and search engine-friendly 3xx code.
Here’s what the header call with the 301 code specified looks like:
header("Location: https://www.examplecom/", true, 301);
What are the key differences between 301 and 302 redirects?
In addition to their permanence, there are a few other factors that distinguish a 301 and 302 redirect from each other. For one, browsers typically cache the page for longer than the session, sometimes even indefinitely. Search engines also typically transfer the page rank to the new location. This establishes 301 redirects ideal for redirecting duplicated content and migrating to a new domain.
Can you set up a PHP redirect without the header function?
The code will look like this:
Things to Keep in Mind When Setting Up PHP Redirects
As we mentioned before, it’s important to have a firm grasp on how to properly set up a PHP redirect before you attempt it. Being cognizant of best practices will help ensure your redirect is successful. Here are a few other things to keep in mind when setting these up.
Relative vs. Absolute URLs
You’re able to use both relative and absolute URLS when creating redirects. However, use caution before including relative redirects. Sometimes website builders collate and then rename the PHP pages. In some cases, while working on your PHP through a website builder, you could ultimately break all of your site’s redirects.
The best way to ensure this doesn’t happen is by keeping a watchful eye of where redirects point.
It’s always a good idea to read the documentation on how to properly use PHP redirects before publishing. This guarantees that you follow best practices and fully understand what you’re achieving.
(Psst: this is also an excellent time to ensure your website is safeguarded from common vulnerabilities. After all, if your site is in the position to be using PHP redirects, that’s a sign that your site’s security can benefit from an audit.)
Set Up PHP Redirects to Help Users and Search Engines
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.
Editor's note: This post was originally published in December 2020 and has been updated for comprehensiveness.