How can PHP be used to redirect based on specific URL endings?

To redirect based on specific URL endings in PHP, you can use the $_SERVER['REQUEST_URI'] variable to get the current URL and then check if it ends with a specific string using the substr() function. If the condition is met, you can use the header() function to redirect to a new URL.

$current_url = $_SERVER['REQUEST_URI'];

if (substr($current_url, -4) == 'page') {
    header('Location: https://example.com/newpage');
    exit();
}