Are there any potential pitfalls to consider when trying to redirect all URLs to a single domain in PHP?

When redirecting all URLs to a single domain in PHP, one potential pitfall to consider is creating an infinite redirect loop if not properly handled. To avoid this, you can check the current domain before performing the redirect to ensure it is not already the desired domain.

if ($_SERVER['HTTP_HOST'] != 'desireddomain.com') {
    header('Location: https://desireddomain.com' . $_SERVER['REQUEST_URI'], true, 301);
    exit();
}