Skip to content

Redirect with PHP, How To

April 1, 2010
You can use a simple PHP script to redirect a user from the page they entered to a different web page. One reason you may want to do this is that the page they are trying to access no longer exists. Using this method, they can be seamlessly transfered to the new page without having to click a link to continue.

  • 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
Difficulty: Easy
Time Required: 2 Minute
Follow it:
  1. <?php
    
       header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    
    ?>

    Change the code on the redirect page to be simply this. You need to replace the URL above with the URL you wish to direct to.

  2. Be sure that you do not have any text sent to the browser before this, or it will not work. Your safest bet is to simply remove all content from the page but the redirect code.
    <html>
    
    <?php
    
       //this will NOT work, the browser received the HTML tag before the script
    
       header( 'Location: http://www.yoursite.com/new_page.html' ) ;
    
    ?>



No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: