How to make a permanent (301) redirect with PHP

Total
0
Shares

Often we need to redirect permanently a page to a new location and this can be accomplished in many ways. I will discuss today the PHP method because this is the one i most often run into.

So you can make a header redirect with php in the following manner:

<?php

header("Location: /example.php");

?>

But keep in mind that this is a temporary redirect. If you need to make it a permanent (301) redirect then you need to add some additional code.

One method is to add an additional response code to the header redirect:

<?php

header("Location: /example.php",TRUE,301);

?>

The other method is a bit longer:

<?php

header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.example.com’);

?>

Both these methods do the same thing so its up to you which one to use.

Leave a Reply

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

You May Also Like