Are there alternative methods to using the header function in PHP for URL redirection to avoid potential pitfalls like incorrect interpretation by external websites?

When using the header function in PHP for URL redirection, there is a potential risk of incorrect interpretation by external websites due to the lack of proper validation and sanitization. To avoid this issue, an alternative method is to use the meta tag refresh in the HTML code to perform the redirection. This method ensures that the redirection is done on the client-side, reducing the risk of misinterpretation by external websites.

<?php
$url = 'https://www.example.com';
echo '<html>';
echo '<head>';
echo '<meta http-equiv="refresh" content="0;url=' . $url . '">';
echo '</head>';
echo '</html>';
exit;
?>