Using Proper Header Redirects In PHP

Mattias Geniar, Wednesday, July 30, 2008 - last modified: Saturday, August 14, 2010

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

Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek, public speaker and podcaster. Currently working on DNS Spy. Follow me on Twitter as @mattiasgeniar.

I respect your privacy and you won't get spam. Ever.
Just a weekly newsletter about Linux and open source.

SysCast podcast

In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!

cron.weekly newsletter

A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!


Alex the Freelance Twin Thursday, July 31, 2008 at 00:59

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.


Tim Ariyeh Thursday, July 31, 2008 at 04:27

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


Craig Francis Thursday, July 31, 2008 at 09:09

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


Matti Thursday, July 31, 2008 at 10:00

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.


sam Friday, September 5, 2008 at 13:40

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.


Rich Friday, February 6, 2009 at 16:38

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.


Matti Friday, February 6, 2009 at 17:40

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.


raj Tuesday, May 5, 2009 at 03:52

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


Ivan Sunday, January 30, 2011 at 11:51

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 to see other status code definitions after reading this post… thought it was helpful.


hi Saturday, February 12, 2011 at 18:25

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


Matti Sunday, February 13, 2011 at 00:33

@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?


carded macbook Monday, June 8, 2015 at 11:24

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.


Leave a Reply

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

Inbound links