July 26, 2007

How to Redirect Using PHP

PHPA simple PHP script could be use to redirect visitor from the page they entered to a different web page. There is lot of reason you may want to do this including:

  • You may have links that is no longer exists in your domain
  • You don't want anybody to browse your parent directory where you don't have any index page in it
  • Or maybe you may want to shorten your long url like TinyUrl.

Using this simple method, they can be seamlessly transfered to the new page without having to click a link to continue.

Why use PHP for redirecting?
Here's some reason on why we are using PHP for redirecting.
  • Users are redirected quickly and seamlessly
  • When using the 'Back' button, the user is taken to the last viewed page, not the redirect page
  • Works on all browsers
However, you have to make sure that your web host do support PHP in order to do this.

In this example, assuming Mikey want to redirect his visitor who go to his domain http://mikey.com to his blog Url which is at http://mikey.com/blog. What mikey have to do is simply create a text file in his parent folder and type this:

header( 'Location: http://mikey.com/blog' ) ;

Then save the file as 'index.php'. Now, all visitors who go to http://mikey.com will be seamlessly redirected to http://mikey.com/blog. Isn't it easy? Enjoy programming and have fun!

Share This Article:

Bookmark This Article:
Feed Me Digg Technorati del.icio.us Best to Stumbleupon Reddit Blinklist Furl Spurl Yahoo Simpy