How can PHP handle redirecting visitors from one domain to another while maintaining the original URL structure?

When redirecting visitors from one domain to another while maintaining the original URL structure, you can use PHP to capture the original URL and then redirect the visitor to the new domain with the same URL structure. This can be achieved by using the $_SERVER['REQUEST_URI'] variable to get the current URL path and appending it to the new domain when performing the redirect.

// Get the current URL path
$current_url = $_SERVER['REQUEST_URI'];

// Redirect to the new domain with the same URL structure
$new_domain = 'https://newdomain.com';
header('Location: ' . $new_domain . $current_url);
exit();