What best practices should be followed when handling URLs and headers in PHP to avoid redirection problems?

When handling URLs and headers in PHP, it is important to ensure that there are no whitespace characters or output before sending headers, as this can cause redirection problems. To avoid this issue, use the ob_start() function to buffer output and headers until they are ready to be sent.

<?php
ob_start();

// Your PHP code here

header('Location: http://www.example.com');
exit;

ob_end_flush();
?>