Redirect a webpage with PHP

There are plenty of reasons to need to redirect a page.  If you do, you here is a sample PHP code snippet that will allow you to point a page to another page on your site or any other site quickly and easily.

This script needs to be the first lines of the script, before any headers or any characters for that matter are sent to the browser.  This means, make sure there is NO SPACE before your opening “<?php” tag.  This happens more frequently than you think, yes, a single simple lonely ” ” space character will make sure this redirect will never work…


<?php

header(“HTTP/1.1 301 Moved Permanently”);
header(“Location:http://newlocation.com“);
exit;

?>

I know it flies in the face of standard coding conventions, but one day we might create a syntax and structure built into our language where a stray space character or semi-colon won’t violate the structural principles and somehow be auto-corrected.  Of course, one may argue that they want a web developer who can notice such details, on the other hand, wouldn’t it be great if your brilliant web developer didn’t have to spend so much time and energy hunting down stray characters that perhaps less experienced coders inadvertently through into their code?  I guess this is one of the best arguments for using a PHP framework since the constructs are tighter so it would be more difficult for developers in a collaborative environment to be taxed with the burden that comes from having too open of a development environment.

Then again, it seems like frameworks such as Symfony and Cake often create more work for simple projects than if your web development team stuck to some standards and developed new content using OOP and MVC patterns.  Using OOP and MVC combined can create an excellent and reliable pseudo-framework but still give your web development team enough flexibility to push new scripts out quickly.

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

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