How can the $_SERVER variable be utilized to manipulate URLs in PHP?

The $_SERVER variable in PHP can be utilized to manipulate URLs by accessing the 'REQUEST_URI' key, which contains the URL path requested by the client. This allows developers to extract specific parts of the URL, modify it, or redirect the user to a different page based on the URL.

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

// Manipulate the URL path (e.g. remove a query parameter)
$newUrl = str_replace('?param=value', '', $currentUrl);

// Redirect the user to the new URL
header('Location: ' . $newUrl);
exit();