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
- Log in to cPanel and open File Manager. Your login is in your welcome email.
- Go to the folder that holds the old page, such as
public_html. - Rename the old file, or copy it somewhere safe as a backup.
- Click + File and create a new file with the old name but ending in
.php, for exampleold-page.php. - Right-click the new file and choose Edit.
- 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.
- Replace the example address with your real new address.
- Click Save Changes.
- 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
.htmlfile, a PHP file with that name will not run. Use a.phpname, or use a redirect rule in.htaccesson 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 thenexit;. - 301 is permanent and 302 is temporary.
- Nothing may be printed before the header.
- For old HTML pages, use
.htaccess.