B2B Marketing Insights by Ironpaper

301 Redirects In PHP

Written by Ironpaper | July 23, 2010

This brief article will provide code samples for creating 301 redirects in PHP.

A 301 redirect is an easy and SEO friendly way to redirect a page that has changed address. For example, if you website recently changed Content Management Systems, and the new CMS generates a different URL construct, then you may want to redirect key pages so the user does not experience an (404) error.

A 301 redirect will also help preserve the PageRank assigned to that page prior to the move. So even if you aren't concerned about visitors stumbling upon a 404 error, you may want to at least tell Google that your page has moved.

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: https://www.example.com" );
?>

Also, redirect with location header:

<?php
header('Location:https://www.example.com');
exit;
exit();
?>

Or, redirect with “Refresh” header:

<?php
header('Refresh: 0; URL=https://www.example.com');
exit;
exit();
?>