What is the recommended method for implementing a 301 Redirect in PHP to redirect one domain to another while keeping the original domain in the address bar?

When implementing a 301 Redirect in PHP to redirect one domain to another while keeping the original domain in the address bar, you can achieve this by using the header() function in PHP. This function sends a raw HTTP header to the client, which in this case will be a 301 Redirect header. By setting the "Location" header to the new domain, the client's browser will be redirected to the new domain while still displaying the original domain in the address bar.

<?php
$redirect_url = 'http://newdomain.com' . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently");
header("Location: $redirect_url");
exit();
?>