Reputation: 173
Good day, everyone!
Client's switching e-shop from one CMS to another. They want to redirect all the old product and category links to new links. Here are the examples of old and new links:
Old links structure:
for categories: http://www.myshop.com/index.php?categoryID=55
for products: http://www.myshop.com/index.php?productID=777
New links structure:
for categories: http://www.myshop.com/categoryName/
`http://www.myshop.com/categoryName/subcategoryName/`
for products: http://www.myshop.com/categoryName/productName/
`http://www.myshop.com/categoryName/subcategoryName/productName/`
About 2000 links total.
As far as i know target CMS would be Virtuemart. Webserver is apache. htaccess and php are supported. Client says they don't want to use htaccess if possible. They prefer to use php script for redirecting all the links.
I've never did so complex url redirecting before and I'd appreciate the help from everyone! I suppose I need to create some php file for that. But what algorithm to use in it and where to put it I don't know. Thanks in advance!
Upvotes: 1
Views: 265
Reputation: 3862
Given your scenario, I would suggest something like this. Note the Moved Permanently header we are using. This is the one you should use as it is the most search engine friendly.
Like the name suggests, PHP redirect tells the browser (or a search engine bot) that the page has been permanently moved to a new location.
<?php
$parent = '';
$child = '';
if (!empty($_GET['categoryID'])){
// Go fetch the category name and
// potential subcategory name
$parent = 'fetchedCategoryName';
$child = 'fetchedSubCatNameIftherewasone';
}elseif (!empty($_GET['productID'])){
// Go fetch the category name and
// potential subcategory name
$parent = 'fetchedProductName';
$child = 'fetchedSubProdNameIftherewasone';
}
$location = '/';
$location .= "$parent/";
if (!empty($child)){
$location .= "$child/";
}
// a more succinct view of this might be:
// header('Location: ' . $location, TRUE, 301);
// here is the verbose example
header("HTTP/1.1 301 Moved Permanently");
header("Location: $location");
exit();
Upvotes: 1
Reputation: 786021
.htaccess
or mod_rewrite
won't be of much help since you will need PHP code to query your database and translate ID to name.
Pseudo code: To be placed on top of your index.php
:
1: Check if $_GET['categoryID']
is not empty
2: If not empty then query your database with supplied categoryID
and get categoryName
3: Place this code on top of index.php
if (!empty($_GET['categoryID']) {
// place sanitization etc if needed
$categoryName = getFromDB($_GET['categoryID']);
// handle no categoryName found here
header('Location: /' . $categoryName, TRUE, 301);
exit;
}
PS: Do similar handling for productID
as well.
Upvotes: 3