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.

Sign Up Now: Free Website Optimization Course from HubSpot Academy

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

header('Location: http://www.example.com/');

exit;

?>

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:

 
<?php

header('Location: http://www.example.com/');

exit;

?>

<!DOCTYPE html>

<html>

    <head>

        <title>Example</title>

    </head>

    <body>

        <p>Hello, world!</p>

    </body>

</html>

Here's a look at an improper location:

 

<!DOCTYPE html>

<html>

<?php

header('Location: http://www.example.com/');

exit;

?>

    <head>

        <title>Example</title>

    </head>

    <body>

        <p>Hello, world!</p>

    </body>

</html>

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.

302 Code

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:

 
<?php

header("Location: https://www.examplecom/", true, 302);

exit();

?>

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:

 
<?php

header("Location: https://www.examplecom/", true, 301);

exit();

?>

PHP Redirect Without Header

If you have issues with the header function, you can still set up a PHP redirect with JavaScript. While the redirect might be slower using JavasScript, it will still be effective. 

Setting up the redirect with JavaScript is simple. You start with the window.location function. Then add an href attribute with the URL you want to redirect users and search engine bots to. Don't forget to add the appropriate attributes to ensure it opens in a new window. Finally, wrap it in <script></script> tags.

Here’s how the code will look using JavaScript:

 
<?php

<script type="text/javascript"> window.location.rel="noopener" target="_blank" href = 'http://www.example.com/';

</script>

?>

There are a few problems with this method, however, in addition to it being slower. First and foremost, JavaScript needs to be enabled and downloaded on the client’s browser for this to work. There are also no status codes involved so you can’t include information about the redirect for search engines. For these reasons, using the header function is considered a best practice.

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.

New Call-to-action

 css introduction

Originally published Dec 7, 2020 7:00:00 AM, updated December 07 2020

Topics:

Website Development