What are the best practices for passing old URLs to new ones in PHP?

When migrating a website or changing URL structures, it is important to implement proper redirection from old URLs to new ones to maintain SEO rankings and ensure a seamless user experience. One way to achieve this in PHP is by using the header function to send a 301 (permanent) redirect header to the new URL. This tells search engines and browsers that the old URL has permanently moved to the new one.

<?php
$oldUrl = "http://example.com/old-page";
$newUrl = "http://example.com/new-page";

header("HTTP/1.1 301 Moved Permanently");
header("Location: $newUrl");
exit();
?>