How To: The Easy 3 Step PHP Redirect

By Bobby Martinez, Thursday, February 12, 2009
Code Image
Photo by Markus Spiske on Pexels

Chances are you’ve seen a link like www.example.com/vote that will link you somewhere, even if it is not on example.com.

If you own your own website, you’ve probably even wondered how to use this to your advantage.

Your initial thought might be, but only an internet wizard of mythical proportions and years of studying the arcane could be powerful enough to wield such magics as to manipulate the interweb in this manner!

Actually, it’s about as easy as slipping into a pair of Hammer Pants.

There are a couple of different ways to accomplish this, but the one I’m going to show you is called a PHP 301 Redirect.

1. First, make a directory in your website that you will want to have the redirect named after. If you wanted to make a link to your favorite singer’s web page, which is most likely MC Hammer, you would make a subdirectory called chamber in the root directory of your website. This means your link will look like this: www.yourdomain.com/mchammer

2. Create a file called index.php in your chamber directory, and insert this code (and only this code!) into the page:

<?

Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.mchammer.com);
?>

3. Replace the example URL (http://www.mchammer.com) with your destination of choice, and save or upload this change. Now, every time you go to www.yourdomain.com/mchammer, you will be redirected to www.mchammer.com.

4. STOP–Hammer Time.

Posted in: Austin Web Design, Tech Support, WWW Learning Center

Comments are closed.