PHP header 301 redirect

This used to be on the php.net reference page, but it’s no longer there. So I’ll post it here for reference.

In order for 301 redirects to work properly in PHP with IE (without showing a Page Not Found error), do the following:

header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$url);
header("Connection: close");

14 Responses to “PHP header 301 redirect”

  1. DontheCat says:

    This comment has nothing to do with this particular post.
    I noticed that you have been blogging about Google.
    Guess you should take a look at my two recent posts.
    Cheers

  2. Jules says:

    This works well. Is it the proper way to handle a ‘file not found’? For example;

    – user requests download.php?file=abc.doc
    – file abc.doc does not exist
    – download.php sends a 301, redirects to an error page

  3. Herbert says:

    Because of the gazillion hits every page we make gets, we can save a lot of cpu-time by replacing the double quotes ” with single quotes ‘
    Well, maybe not that much.. :P

  4. Thanks for this, Elliot. Herbert may be right, code optimisation is all about the little things! I implemented this with single quotes and no variable – though of course this would not suit you if you need a dynamic URL.

  5. Çelik çat? says:

    Thanks you too much mans! Love you Intelliot!

  6. Hua Chen says:

    I don’t know the useage of header(“Connection: close”);? What does it do?

  7. sjolzy says:

    Thx, I just forgot how to write it

  8. Maquetador says:

    Good tips. I have never user ‘ connection: close’

Leave a Reply