Sending a 301 “Moved Permanently” Header with PHP

Posted September 3rd @ 8:26 am  |  Filed in: PHP    

Though I briefly talked about this in another post, I use this so often I thought it deserved a post of its own.

To send this particular header, simply put this PHP code before there’s any output to the page:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.somesite/some-page/");
?>

If you’re using Apache, you can also do this in an .htaccess file. But sometimes this is easier.

Why Use 301 Headers?

First off, there’s the benefit to your visitors - this will shoot them over to the updated page right away. But the real benefit of using this type of header is that is lets search engines know that this is a permanent change of location for this page, and that they should update their records accordingly. Thus, the new location of the page will start showing up sooner than later in search engine result pages (SERPs), and you’ll stand a better chance of retaining some page rank.

Cheers!

1 Trackbacks/Pingbacks

  1. Pingback: 5 PHP Mistakes and How to Avoid Them on April 14, 2009

2 Comments

  1. http://www.foulplay.se » Micke said on August 23, 2010 at 12:15...

    Is it possible to force e.g. Google to update with the new link or is header 301 the only option?

  2. http://www.stoptimestudio.com » Matt said on August 23, 2010 at 12:54...

    @Micke If nothing else is done, Google will eventually wipe out the old page’s record and add a brand new record for the new version of the page (so yes, it will update itself with the new link). The reason to use a 301 header is to help the search engine maintain a relationship between the old and new versions of the page, which is beneficial. So, to answer your question, you don’t need to do it this way, but it’s the best way to do it.

Leave a comment

OpenID Login

Standard Login

Options:

Size

Colors