How can PHP be configured to correctly display the requested URL in an email notification for a 404 error page?

When a 404 error occurs, PHP can be configured to send an email notification including the requested URL that triggered the error. To achieve this, you can use the $_SERVER['REQUEST_URI'] variable to capture the requested URL and include it in the email message.

// Check if a 404 error occurred
if (http_response_code() == 404) {
    // Get the requested URL
    $requested_url = $_SERVER['REQUEST_URI'];

    // Send an email notification with the requested URL
    $to = 'admin@example.com';
    $subject = '404 Error Notification';
    $message = 'The following URL triggered a 404 error: ' . $requested_url;
    $headers = 'From: webmaster@example.com';
    
    mail($to, $subject, $message, $headers);
}