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 be used to redirect users to a login page for password verification before allowing them to post in a Shoutbox?
- How can PHP scripts recognize which checkbox was activated when passing information through a POST form?
- How can one prevent the destruction of database files when working with dbase in PHP?