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";
}
Keywords
Related Questions
- How can error reporting settings impact the detection of issues like failed includes in PHP scripts, and what steps can be taken to ensure errors are properly displayed?
- What are the potential pitfalls of using ereg_replace in PHP scripts?
- How can PHP developers ensure that their email headers are RFC compliant to prevent issues with email delivery and content display?