What are the security implications of domain redirection and how can they be mitigated?

Domain redirection can lead to security risks such as phishing attacks, where malicious actors redirect users to fake websites to steal sensitive information. To mitigate these risks, ensure that the redirection is done securely using HTTPS, validate user input to prevent open redirection vulnerabilities, and regularly monitor and audit the redirection process for any suspicious activity.

// Secure domain redirection using HTTPS
if ($_SERVER['HTTPS'] != 'on') {
    header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

// Validate user input for redirection
$allowed_domains = ['example.com', 'example2.com'];
$redirect_domain = $_GET['redirect'];

if (in_array($redirect_domain, $allowed_domains)) {
    header('Location: https://' . $redirect_domain);
    exit();
} else {
    // Redirect to a safe page if input is not allowed
    header('Location: https://safe-page.com');
    exit();
}