Are there best practices for handling URL redirects in PHP to avoid duplicate content issues?

When handling URL redirects in PHP to avoid duplicate content issues, it is important to use 301 redirects to indicate to search engines that the content has permanently moved. This helps consolidate link equity and prevent duplicate content penalties. Additionally, make sure to redirect all variations of a URL to a single canonical URL to avoid confusion for search engines.

// Redirect to the canonical URL
if ($_SERVER['REQUEST_URI'] == '/old-url') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: https://www.example.com/new-url');
    exit();
}