問題描述
301 重定向舊 osCommerce 鏈接的最佳方式 (Best way to 301 redirect old osCommerce links)
We are in the process of migrating from an old osCommerce install to WordPress. 90% of the traffic is sent from organic results, and with over 4,000 indexed product pages, redirecting to the new site is essential.
The old site's URL structure:
/product_info.php?cPath=1&products_id=4
where cPath
=> osCommerce category and products_id
is just that. both of these are contained in the osCommerce DB
The new site:
/categoryname/product-title
What's the best way to dynamically 301 all of these URLs to the right place since we are moving from IDs to permalinks?? I was thinking maybe a PHP script to generate the .htaccess file, does anyone have experience with that? I can map the category IDs to the correct WordPress category slug, and scrape the old site to match the old product URLs with the product title. Is this the best way/even possible? Banging my head against a wall here.
I've looked at this but it doesn't apply because I'm trying to redirect all of the old products dynamically, or at least generate the redirects dynamically (like I said, about 5000 products) Rewrite htaccess old oscommerce links
EDIT: My idea:
1. Create .htaccess file.
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=([0-9]+)&products_id=([0-9]+)
RewriteRule ^(.*)$ http://www.domain.com/redirect.php?cat=%1&product=%2? [R=301,L]
2. It sends all matching requests to a custom php 301 redirect script
3. Script contains an array of category_names => id pairs
already mapped to the correct database values (no database calling), and a separate array of products_names => product_ids
4. After matching the category and product information, the script pushes you to the right place:
$yourNewLocation = $cat . '/' . $product . '/';
function return_301($yourNewLocation){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '. $yourNewLocation);
}
Possible bottlenecks: associative arrays of 5000+? is that a major bottleneck vs a mysql join to get the needed info?
參考解法
方法 1:
"I was thinking maybe a PHP script to generate the .htaccess file"
If you are going to do that, I suggest placing your redirects in an httpd include rather than the .htaccess, there is a performance hit when using .htaccess - an include will probably be be better in that respect.
Also if you are going to map ids to permalinks using php:
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$yourNewLocation);
I'm not 100% sure if your plan is 'the best way' but it certainly does sound reasonable.. (of course redirecting dynamically with php + a database call will be more of a hit than wither .htaccess or include methods!)
(by timelf123、Sean Kimball)