Saturday, May 4

PHP Redirect Code

PHP Redirect 301 permanently

Sometimes you might want to redirect your visitors to a new URL address. This article will show you how to make a PHP redirect using the 301 "moved permanently" redirection. This is the one you should use as it is the most search engine friendly. Like the name suggests, PHP redirect tells the browser (or a search engine bot) that the page has been permanently moved to a new location.

PHP Redirect Code

To redirect people and robots to a new location use this PHP redirecting code:
header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.New-Website.com"); ?>
Tip: use lower-case name for the header function (not Header) to make sure your PHP redirect code is compatible with PHP 6.

You could skip the 301 Moved Permanently tag and use just:
header("Location: http://www.New-Website.com"); ?>
But this would result in a "302 Moved Temporarily" redirect instead of a 301 one. This should be avoided as permanent redirects are more search engine friendly and should be used where possible.

You can enter any sub-page for the location, this PHP code will redirect users to the test.php sub-page of your website:
header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.New-Website.com/test.php"); ?>

It is important that you don't have any other code (including empty rows and spaces) before the above PHP redirect code. If you do you will get a nice headers already sent notice from PHP and the redirect will not work.

That's it! Enjoy redirecting PHP pages.

Copyright notice

This tutorial is copyrighted by Gastd. Obtain permission before copying, re-publishing or otherwise redistributing this article.

No comments: