What are the best practices for handling URL redirection in PHP, especially for different domains?
When handling URL redirection in PHP, especially for different domains, it is important to ensure that the redirection is done properly to avoid any potential issues with SEO or user experience. One best practice is to use the HTTP status code 301 for permanent redirects and 302 for temporary redirects. Additionally, it is recommended to include the full URL including the protocol (http:// or https://) when redirecting to a different domain.
<?php
// Redirect to a different domain with a 301 status code
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.newdomain.com/newpage.php");
exit;
?>