Support article
Redirect a website with PHP easily
Learn how to create a PHP redirect with header(), when to use 301 or 302, and which mistakes you should avoid.
Introduction
Redirecting one URL to another is very common when you change your website structure, replace an old page or send visitors to an updated address.
If you work with .php files, one of the simplest ways to do it is with the header() function.
What is a PHP redirect
A PHP redirect is an instruction inside a .php file that tells the browser to load a different URL.
For example, someone may visit:
https://yourdomain.com/old-page.php
and be sent automatically to:
https://yourdomain.com/new-page.php
How to redirect a page with PHP
1. Open the correct PHP file
Access the file that receives the visit, usually from DirectAdmin, cPanel or FTP.
2. Add the redirect at the beginning of the file
The basic code looks like this:
<?php
header("Location: https://yourdomain.com/new-page.php");
exit;
?>
It should appear before any HTML, text or blank spaces.
3. Use 301 if the change is permanent
If the old URL will no longer be used, a permanent redirect is usually the right choice:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://yourdomain.com/new-page.php");
exit;
?>
4. Use 302 if the change is temporary
If the redirect is only for a short time, you can use:
<?php
header("HTTP/1.1 302 Found");
header("Location: https://yourdomain.com/temporary-page.php");
exit;
?>
5. Test the URL
After saving the file, open the old URL and confirm that it goes directly to the new page.
Common mistakes
Headers already sent
This usually means there is output before header(), such as spaces, HTML or line breaks.
Forgetting exit;
It is good practice to add exit; so the rest of the PHP code does not keep running.
Creating redirect chains
Try to avoid old URL -> middle URL -> final URL chains. Direct redirection is cleaner.
Useful tips
- Send users to a related page. The destination should match the original intent of the old URL.
- Keep a backup before editing files.
- Use 301 for permanent replacements.
- Check that the final URL works correctly with HTTPS.
- Use
.htaccessfor larger redirect sets.
Frequently asked questions
Can I redirect to another domain
Yes. The destination can be on the same domain or a completely different one.
Is PHP better than .htaccess for redirects
It depends. PHP is practical for individual files. .htaccess is often better for many rules or site-wide changes.
Does a PHP redirect affect SEO
It can, especially if you choose the wrong redirect type or send users to an unrelated page.
Conclusion
Redirecting a page with PHP is a simple solution when you work with .php files and need to send visitors from one URL to another.
If you choose the right redirect type and place the code correctly, it is an easy and effective way to keep your website organized.