What are the best practices for handling URL redirects when changing from a system that uses product IDs to one that uses descriptive URLs in PHP?

When transitioning from a system that uses product IDs to one that uses descriptive URLs in PHP, it is essential to set up URL redirects to ensure that existing links and bookmarks continue to work properly. This can be achieved by creating a mapping between the old product IDs and the new descriptive URLs, and then using server-side redirects (e.g., 301 redirects) to direct incoming requests from the old URLs to the new ones.

// Sample code for handling URL redirects in PHP

// Define an array mapping old product IDs to new descriptive URLs
$redirects = array(
    '123' => 'new-product-url-1',
    '456' => 'new-product-url-2',
    // Add more mappings as needed
);

// Check if the requested URL corresponds to an old product ID
if (isset($redirects[$_GET['product_id']])) {
    // Perform a 301 redirect to the new descriptive URL
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: https://www.example.com/' . $redirects[$_GET['product_id']]);
    exit();
}

// Handle the request for new descriptive URLs
// Add your logic here to display the content for the new URLs