What is the purpose of using PHP to remove file extensions in an existing script and how can this be implemented effectively?

When removing file extensions in an existing script, the purpose is to clean up URLs and make them more user-friendly. This can improve SEO and make the website more accessible to users. To implement this effectively, you can use PHP to parse the URL, remove the file extension, and redirect the user to the correct page.

<?php
// Get the current URL
$current_url = $_SERVER['REQUEST_URI'];

// Remove the file extension from the URL
$clean_url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $current_url);

// Redirect the user to the clean URL
header("Location: $clean_url");
exit();
?>