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.

The Social Box

You can sign up for more updates via Twitter or Facebook below. On Twitter, I regularly talk about technology or tweet about interesting stories. Topics that don't necessarily make it to this blog. Facebook contains a steady update of blogposts and some more lightweight stories.

The @mattiasrss account has an automated RSS feed of all blogposts that get published.




Write a Comment

Do you care about the markup if your comment? You can use the following HTML tags:

<code>command</code>: command highlighting
<pre>text</pre>: pre-formatted code, can be multi-line (black background, white letters)

example <pre> tag
<blockquote>text</blockquote> quoted text
quoted example


None of this is needed of course, it's all optional!

Comment

*

12 Comments

  1. 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).

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. You will start following the step-by-step tutorials
    and create your’s today. Google Voice is one
    of the easiest services to use for i – Phone call recording and free.
    Most those who own i – Phone’s are notable for having an original style, and also a type that’s sleek and clever.