What are the potential consequences of having a webpage accessible through multiple URLs in PHP?

Having a webpage accessible through multiple URLs can lead to duplicate content issues, which can negatively impact your site's search engine rankings. To solve this issue, you can use PHP to redirect all non-canonical URLs to the preferred URL using a 301 redirect.

<?php
// Redirect non-canonical URLs to the preferred URL
$canonical_url = "https://www.example.com/preferred-url";
$current_url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if ($current_url != $canonical_url) {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $canonical_url");
    exit();
}
?>