What is the best practice for preparing static content before using it in a header location redirect?

When preparing static content before using it in a header location redirect, it is important to ensure that the content is properly sanitized and validated to prevent any potential security vulnerabilities. This can be done by using functions like htmlspecialchars() to escape special characters and prevent XSS attacks. Additionally, it is recommended to check for any user input or dynamic data that may be included in the redirect URL to avoid any unintended redirections.

// Example of preparing static content before using it in a header location redirect

// Static content to be used in the redirect
$redirectUrl = 'https://example.com';

// Sanitize the redirect URL
$redirectUrl = htmlspecialchars($redirectUrl, ENT_QUOTES, 'UTF-8');

// Perform any additional validation checks if needed

// Redirect using header location
header('Location: ' . $redirectUrl);
exit;