PHP Header Redirects

There’s a few ways to redirect a page in PHP. Well, two. A right and a wrong.

Wrong Way

header('Location: foo.php ');

Right way

header('HTTP/1.0 302 Found');
header('Location: foo.php ');
exit;

This is evident on IIS6 and IIS7 where the wrong redirect will just fail.

Even if you are not using IIS6 or IIS7 its good practice to set the headers correctly and exit the script.

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

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