Using Proper Header Redirects In PHP

Let's say you have an old web-page that's no longer in use. Of course, you'll want to redirect your users to the correct (new) page. Using PHP, you can modify the headers of a web-page to redirect them to the correct page.

This will cause the user who visits the web page with this code, to be redirected to http://www.mydomain.com/newpage.html (or any other page you specify).

That's all fine, and works well. But what if Google indexed your old web page, and still directs traffic to it? What if search-results in Google only display the old page, instead of the new one? What if, one day, you decide to take the old page offline?

To better understand the next part, let's first explain what status codes are and what they mean to us, and Google. When a web page is successfully loaded, it sends your browser (IE, Firefox, ...) a status code of "200", which means: the page exists, and was sent successfully. There's also a status code called "404", which means the page you requested doesn't exist. It's these status codes that Google uses to keep track of pages that are moved, deleted or temporarily unavailable.

By default, a header("Location: ...")-redirect will pass a 302 status code, which means the page was moved temporarily, but will be back shortly. If Google sees such as page, it'll keep it in its index, as it assumes it will once exist again. In this case, it's better to pass a 301 status code, to explain the page moved permanently to another location.

To pass a 301 status code to the browser, you can do the following.

Once Google crawls that page, it'll see it was moved permanently, and remove the current page from its cache (though this can take a while), and start displaying the new link in the search results -- instead of the old one.

Another method to pass status codes is to use the additional parameters of the header() function. Doing the following, is essentially the same as the previous example, redirect to another page with a 301 -- Moved Permanently status code.

You can do the same if you display a custom 404 page (by using .htaccess and the ErrorDocument directive, for example). By passing the necessary headers, you can tell the browser (and thus all search-indexing-bots out there) to no longer link to this page, as it doesn't exist.

And you can still display your custom error page, to notify your visitors as well.

Using headers in PHP is definitely something worth spending a few minutes on, as it can help you create clean redirects/error pages, and keep your indexed pages limited to those that actually exist, and contribute to your website.

There's a very useful Firefox Plugin called LiveHTTPHeaders that can help you test these headers, and see if they are used properly. It'll show you the statuscode of the website you're visiting, and can easily display 301, 302, 404, ... statuscodes.

Looking for help?

Tired of fixing all these tech-problems yourself? We've got an excellent team at Nucleus, a top-class Belgian hosting provider, that can help you. Discover our Managed Hosting, where skilled engineers manage your servers and keep them up-to-date, so you can focus on your core business. We use a variety of Configuration Management Systems such as Puppet to make sure every config is reviewed, unit-tested and guaranteed to be working.

Want to get in touch? Find me as @mattiasgeniar on Twitter or via the contact-page on my blog.

Tagged with: ,
Posted in php, Seo
11 comments on “Using Proper Header Redirects In PHP
  1. Thanks for the tips. I usually use my .htaccess for perm redirects, but I do use header() a lot too. So I think im going to switch over to using Header with 301 in there.

  2. Tim Ariyeh says:

    Excellent points. If we could count on consistently getting valid return codes, we could also take them into account when consuming web services and using things like cURL

  3. Remember to call exit() after a header()… I’ve seen cases where php scripts use the header() to redirect on a specific condition (i.e. successful processing of a form, before redirecting to a thank-you page), and although the browser does not show it, the script continued to execute (i.e. printing the whole data entry form again).

  4. Matti says:

    I’ve seen the same kind of behaviour as well, headers not being processed until (parts of) the page have been displayed. I’ll add it in the blog-post as well, it’s certainly something to keep in mind.

  5. sam says:

    I have used the header bt it still gives the error…
    n im unable to understand bt the error is.Actually I have a form when i edit the values of the form , the new updatd values should be saved in the database and in turn also saved in the form. Im unable to redirect these new values to the other form.
    kindly send me a suitable rply for this problem.

  6. Rich says:

    I have a question. What if my old site has a great position in google because of content, keywords etc but the new site does not contain the sma content, but i want to redirect and keep the good position,

    basically I seo one site and then redirect it to one (less seo) will google still index old site because i want it to.

  7. Matti says:

    It won’t, because by redirecting you’re telling searchbots that the current page no longer exists, and the content should be found on another page. The “type” of redirection (permanent/temporary) only specifies the relation.

  8. raj says:

    My site which is using https gets redirected to http when header function is used. Ahts the solution to avoid that

  9. Ivan says:

    Thank you for the great overview! I was wondering if I should specify a status code for some of my headers, but now that I know the default is 302 it looks like I don’t need to. I looked at w3.org to see other status code definitions after reading this post… thought it was helpful.

  10. hi says:

    i am not able to redirect to a file (that is in my directory) in php

  11. Matti says:

    @Previous: you’re going to give a bit more info. What files do you have, what’s their content, and what kind of redirect do you want?

3 Pings/Trackbacks for "Using Proper Header Redirects In PHP"
  1. [...] more information on what Redirect-Code to use ([R=301]) I kindly refer you to the following page: Using Proper Header Redirects In PHP. There you’ll find a good explanation on the difference in 301 and 302 redirects, and their [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>