Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to create a PHP redirect, step by step

A redirect sends visitors from an old page to a new one. This guide shows how to make one with a few lines of PHP.

Web Hosting2 min read9 steps

What is a redirect?

A redirect works like mail forwarding. When someone asks for the old address, the server tells their browser to go to the new address. The visitor lands on the right page without doing anything.

Before you start

  • Know the old page and the new address.
  • Your hosting must support PHP. PHP is a language that runs on the server.
  • Back up the old file first.

Steps

  1. Log in to cPanel and open File Manager. Your login is in your welcome email.
  2. Go to the folder that holds the old page, such as public_html.
  3. Rename the old file, or copy it somewhere safe as a backup.
  4. Click + File and create a new file with the old name but ending in .php, for example old-page.php.
  5. Right-click the new file and choose Edit.
  6. Type this code:
<?php
header("Location: https://example.com/new-page/", true, 301);
exit;
?>

This tells the browser to move to the new address. The code 301 means the move is permanent. Use 302 for a short-term move.

  1. Replace the example address with your real new address.
  2. Click Save Changes.
  3. Open the old address in your browser. You should land on the new page.

Rules to remember

  • Put the code on the very first line. Do not leave blank lines or text before <?php.
  • Keep exit; after the header so PHP stops.
  • If the old page was an .html file, a PHP file with that name will not run. Use a .php name, or use a redirect rule in .htaccess on Apache instead:
Redirect 301 /old-page.html https://example.com/new-page/

This one line does the same job for an old HTML page.

Tip: Browsers remember 301 redirects. Test in a private window to see your newest change.

Quick recap

  • Use header("Location: ...") and then exit;.
  • 301 is permanent and 302 is temporary.
  • Nothing may be printed before the header.
  • For old HTML pages, use .htaccess.