How to Safely Perform a PHP Redirect?

php redirect

If you’ve heard of a URL redirection or URL forwarding, you are familiar with a PHP redirect is. If not or if you require a refresher, a redirect is the forwarding or rerouting of a web page, form or website. For example, you searched for monsterhost.com/seotool, but are instead sent or redirected to monsterhost.com/seo. Redirects are usually done to accomplish several goals including re-routing specific web pages to new locations, managing website traffic during site maintenance or during downtime or website backup.

A redirect is also done when you want to get rid of the ‘www.’ portion of a URL, modify the URL structure for a site or to permanently forward users to a different website.

Reasons to perform a PHP redirect

There are a number of reasons redirects are used. These range from dealing with website traffic during downtime or site maintenance; re-routing a specific page to a new location or getting rid of the “www.” Portion of a URL.

If you are looking to alter your website’s URL structure or permanently forward users to a different website, a redirect will be necessary.

How to make a PHP redirect?

Setting up a PHP redirect is a fairly simple process and the header function is crucial. Start by creating an index.php file  in the directory that you want to redirect from.

Use the content below to achieve that.

<?php header("Location: http://www.redirect.to.url.com/"); ?>

It can also be written in the following format:

<?php header("Location: anotherDirectory/anotherFile.php"); ?>

The ‘http://www.redirect.to.url.com/’ or the, anotherDirectory/anotherFile.php”); ?> portion of either file is where you’ll enter the URL that you want users to be redirected to. It is important to note also that file type is not limited to HTML and can range from php, perl, python, cgi to compiled cgi programs.

Four best practices for a PHP redirect

1. Use a Relative URL

ssl header

There are different types of redirects, such as SSL and PHP which we are exploring in this post. There are also different types of URLs used to perform these redirects. These include relative and absolute URLs.

An absolute URL is one that has a host name, while a relative URL is one that does not have the host name. Relative URL was only standardized as part of HTTP six years ago so its fairly new and works in modern browsers. But to be on the safe side, when doing a redirect, it’s advised that you redirect to an absolute URL.

2. Call the header function before you write anything to the browser.

This is super important for a successful redirect. The browser has to receive the HTTP header first for the redirect to work. This could even be a blank line before the PHP opening tag. Don’t forget this, or it will result in an error message.

3. Exit the script immediately after you’ve redirected.

This is more of a good practice than it is a requirement. But a very important one. Remember that the script will continue performing even after you sent the header.

That by itself doesn’t seem like a big deal, but what happens is that the although the script is executing, the browser doesn’t ever see the results of what comes after the redirection.

4. Send HTTP status codes based on redirection type

There are different types of redirects. Some, like the 302 are temporary, while others, like the 301 are permanent. It’s crucial that the status code sent is in alignment with the redirect you are aiming for.

If this is not specified, a 302, which is a “Found” or “Moved Temporarily”

is generally generated using the fore bug browser plugin.  This is ok if you are looking to temporarily redirect, but if you want your redirect to be permanent or something else, this is not good news. To avoid that, be specific with your status code.

Summary

There are a range of reasons to do a redirect. Whether you want to cater to your users while carrying out maintenance or while doing a complete overhaul of your site or a single page. But whatever your reason is, it’s important to follow the best practices noted to get the best result.

The process of redirecting is fairly simple. However, if you are not up for the work or you want guaranteed results, go ahead and contact your webhosting provider or website provider for assistance.