How can syntax checking and listing virtual hosts help in troubleshooting PHP redirect issues?

When troubleshooting PHP redirect issues, syntax checking can help identify any errors in the code that may be causing the redirect to fail. Listing virtual hosts can help ensure that the correct host configuration is being used for the redirect to work properly.

// Example of syntax checking in PHP
$redirect_url = "https://example.com";
if(filter_var($redirect_url, FILTER_VALIDATE_URL)){
    header("Location: $redirect_url");
    exit();
} else {
    echo "Invalid URL";
}

// Example of listing virtual hosts in Apache configuration
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
</VirtualHost>