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

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;

Back to redirect

Comments

Leave a Reply

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