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();
}
Related Questions
- Is it a good practice to nest substr() with strpos() and strrpos() in PHP code?
- In PHP, what considerations should be made when converting integers to floats for accurate calculations?
- What best practices should be followed when creating a survey with PHP to ensure proper handling of sessions and form elements?