How to Redirect With PHP

Use This Redirect Script to Forward to Another Page

 iStock/Getty Images Plus

A PHP forwarding script is useful if you want to redirect one page to another so that your visitors can reach a different page than the one they land on.

Fortunately, it's really easy to forward with PHP. With this method, you seamlessly transfer visitors from the web page that no longer exists to the new page without requiring them to click a link to continue.

How to Redirect With PHP

On the page that you want to redirect elsewhere, change the PHP code to read like this: 

<?php header( 'Location:' ) ; ?>

The header() function sends a raw HTTP header. It must be called before any output is sent, either by normal HTML tags, by PHP, or by blank lines.

Replace the URL in this sample code with the URL of the page where you want to redirect visitors. Any page is supported, so you can transfer visitors to a different webpage on your own site or to a different website entirely.

Because this includes the header() function, be sure that you do not have any text sent to the browser before this code, or it will not work. Your safest bet is to remove all the content from the page except for the redirect code.

When to Use a PHP Redirect Script

If you remove one of your web pages, it's a good idea to set up a redirect so that anyone who bookmarked that page is transferred automatically to an active, updated page on your website. Without the PHP forward, visitors would remain on the dead, broken, or inactive page.

The benefits of this PHP script are as follows:

  • Users are redirected quickly and seamlessly.
  • When the Back button is clicked, visitors are taken to the last viewed page, not the redirect page.
  • The redirect works on all web browsers.

Tips for Setting up a Redirect

  • Remove all code but this redirect script.
  • Mention on the new page that users should update their links and bookmarks.
  • Use this code to create a drop-down menu that redirects users.