HTTP 301 Redirect using PHP

You can use the PHP header() function to do redirect.

To do 301 redirect, use

header('Location: https://new-url/', true, 301);
exit;

For 302 redirect, use

Advertisement

header('Location: https://new-url/');
exit;

If you need to redirect a site to a new site while keeping the same URL structure, you can use

$newUrl = "https://NEW-URL-HERE" . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently");
Header("Location: $newUrl");
exit;
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement