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();
?>
Related Questions
- What are some best practices for handling text formatting and output in PHP forms to ensure proper display on webpages?
- When should public variables in PHP classes be changed to private or protected for better code design?
- What is the best practice for executing a Powershell script as an administrator within a PHP script on a Windows Server?