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();
}
?>
Related Questions
- How can PHP developers effectively troubleshoot and debug issues related to retrieving and counting data from multiple database tables?
- What are common issues that PHP beginners face when setting up projects locally using Xampp?
- What is the purpose of using array_push in PHP when adding elements to an array?