What are the best practices for handling domain redirection in PHP?

When handling domain redirection in PHP, it is important to ensure that the redirection is done correctly to avoid issues such as duplicate content or broken links. One best practice is to use 301 redirects, which indicate to search engines that the page has permanently moved to a new location. Additionally, it is recommended to use server-side redirection methods rather than client-side techniques to ensure proper redirection for all users and search engines.

<?php
// Redirect to a new domain using a 301 redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.newdomain.com");
exit();
?>