PHP redirect code

Author: Klemen Stirn

So, how do you make PHP redirect your visitors to another page? A 301 “moved permanently” is the redirect you should use as this is the most search engine friendly one. Like the name suggests it tells search engines (and browsers) that a page has been permanently moved to a new location.

PHP redirect code

To redirect people and robots to a new location use this PHP redirect code:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.YourNewWebsite.com”);
?>

If you used just…

<?php
header(“Location: http://www.YourNewWebsite.com”);
?>

… this would result in a “302 Moved Temporarily” redirect instead of a 301 one.

You can enter any sub-page for the location, this PHP code will redirect users to the test.php sub-page of your website:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.YourNewWebsite.com/test.php”);
?>

It is important that you don’t have any other code (including any empty rows or 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!

Share
  • Share/Bookmark
Related Posts Related Websites

About the Author

Ahmed Abdul Ghany