PHP 301 permanent redirect

PHP 301 permanent redirectPHP 301 permanent redirect can be used if it is necessary to move a page on your php web server from one location to another. For example, we had a few articles published under certain categories of our website, but when we redesigned the website we added different categories and some articles fitted better in the new categories.

There are three options here (and only one of the three is correct):

New Page Redirect

  • Delete the old page and simply move it
  • Leave the old page and copy to the new page
  • Move the page to the new location and use a 301 redirect page in the old location.

Option 1 is incorrect for a few reasons. One is that search engines may have that site in their index and when they cannot find it any more the trust of your site diminishes a little. Another reason is that some people may have bookmarked that page, or other web masters may have linked to that page, so simply deleting it will make your site look unprofessional.

Option 2 is incorrect too. There are two reasons I say that. One reason is that the search engines do penalise website for duplicate content and another reason is that if you make changes to one article, you will not remember to make them to the second one.

Option 3 is the clear winner. What a 301 redirect does is it tells the requesting web browser or search engine that the page has permanently moved to a new location, then it redirects them to the new location automatically.

The gets around the broken links and bookmarks problem and it also gets around the duplicate content problem

So, how do you do a PHP 301 permanent redirect?

Well, on our php web server that’s easy to do. Lets assume you have a file at www.site.com/old_directory/page.php and you move it to www.site.com/new_directory/page.php.

The page.php in the new_directory will be the old page which has moved to its new location (note, this could have been in the same old_directory, but with a new page name, eg, www.site.com/old_directory/MyCoolPage.php).

The 301 Permanent Redirect Headers in PHP

Change the code in the old page to look like this:

<?php
// Permanent redirection
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.site.com/new_directory/page.php”);
exit();
?>

The first header lets the web browser or search engine know that the page has moved from one location on the web hosting server to another. The second header takes the browser or search engine to the new page on the web hosting server.

This is the best and easiest way of redirecting pages when they move on your server (you could also use .htaccess, but not all web hosting providers give access to .htaccess).

facebook google twiter linkedin linkedin linkedin linkedin linkedin

Leave a Reply

Have a Quesion?

Questions? We're here. Send us a message!

Questions, issues or concerns? I'd love to help you!

Click ENTER to chat