In what ways can PHP be utilized to efficiently map old product IDs to new URL slugs for SEO purposes when transitioning to a new shop system?

When transitioning to a new shop system, it's important to maintain SEO by mapping old product IDs to new URL slugs. PHP can be utilized to efficiently create a mapping array that stores old product IDs as keys and new URL slugs as values. Then, when a request is made with an old product ID, the PHP code can redirect to the corresponding new URL slug.

// Create a mapping array of old product IDs to new URL slugs
$products_mapping = [
    'old_product_id_1' => 'new-url-slug-1',
    'old_product_id_2' => 'new-url-slug-2',
    // Add more mappings as needed
];

// Get the requested product ID
$requested_product_id = $_GET['product_id'];

// Check if the requested product ID exists in the mapping array
if (array_key_exists($requested_product_id, $products_mapping)) {
    // Redirect to the new URL slug
    $new_url_slug = $products_mapping[$requested_product_id];
    header("Location: https://www.example.com/$new_url_slug", true, 301);
    exit();
} else {
    // Handle cases where the product ID is not found in the mapping
    echo "Product not found";
}