How can PHP developers ensure that their website's content is accessible under a single URL to improve SEO ranking?

To ensure that a website's content is accessible under a single URL for improved SEO ranking, PHP developers can implement a 301 redirect from non-canonical URLs to the preferred URL. This helps search engines understand which URL should be indexed and avoids duplicate content issues. By setting up this redirect, developers can consolidate link equity and avoid diluting the SEO value of their content.

<?php
if ($_SERVER['HTTP_HOST'] != 'preferreddomain.com') {
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://preferreddomain.com' . $_SERVER['REQUEST_URI']);
    exit();
}
?>