PHP Redirect

Download Free

There are multiple ways to redirect users from one url to another, but our favorite is with a simple PHP Redirection script. Here is a quick example followed by advanced examples that include variable pass throughs.

Make sure that the script is located at the top of the page, and that it proceeds any html, javascript, or other php.

Standard Strict PHP Redirect

<?php
header( 'Location: http://yourwebsites.com/newpage.php' ) ;
?>

If you have short_open_tag enabled then you can use the code below as well.

Standard Non-Strict PHP Redirect

<?
header( 'Location: http://yourwebsites.com/newpage.php' ) ;
?>

You can see from the example above that the letters php are missing from the opening tag <? if your php configuration is setup to allow short_open_tag or short tags, then this will work fine.

Variables PHP Redirect

<?
$page = $_GET['item'];
header( 'Location: http://yourwebsites.com/'.$page.'.php' ) ;
?>

You can pass variables to php to have a single page redirect to more than 1 page. Going to this page http://yourwebsites.com/newpage.php?item=taco would pass the variable taco to the script above and then the user would be reidrected to http://yourwebsites.com/taco.php

We hope this has been of use to you, and if it has please bookmark and share it. Thanks!