1

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?

Community
  • 1
  • 1
timelfelt
  • 680
  • 1
  • 11
  • 26
  • I would do it in **PHP**. If it will be done via Apache's mod_rewrite, it will take 2 lines for each redirect rule (`RewriteRule` for actual redirect and `RewriteCond` to match specific query string). – LazyOne Jul 25 '11 at 00:29
  • 1
    I don't know if it is really good idea to use PHP array for this (it will be quite big array .. and PHP arrays are quite inefficient in terms of memory consumption). The speed should be fine (or at least similar to MySQL, but requires testing as each server is different) but memory consumption definitely will be high .. which may become a bottleneck if you are on shared hosting and having lots of redirects at the same time. I would go MySQL to store redirect pairs. – LazyOne Jul 25 '11 at 10:27
  • 1
    1) There is no need for 1st RewriteCond -- the pattern can be easily moved into RewriteRule itself (this will also reduce the number of matching as well -- not a big gain, but why having this extra if it can be **easily** avoided). 2) I don't know all the details .. but I think there is no need for rewrite rule AT ALL - just use `product_info.php` instead of `redirect.php` in first place (place all redirect code there). – LazyOne Jul 25 '11 at 10:29

1 Answers1

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!)

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • Thanks! What about a combination of the two? The .htaccess could send `/product_info.php?cPath=1&products_id=4` to `/my301generator.php?cPath=1&products_id=4` , which would then use premapped arrays (already matched to the DB, `'cPath 1' -> 'CategoryName'`) to spit out the correct `$yourNewLocation`. This means after mapping to the DB, I don't need to make any DB calls and everything is dandy, no? Looking into the `RewriteCond` rule now – timelfelt Jul 25 '11 at 06:55
  • I took a stab at what I was going for in the original post – timelfelt Jul 25 '11 at 07:30
  • I would think that a efficiently written query is going to be far more efficient than parsing a 'mappings' file. – Sean Kimball Jul 25 '11 at 14:31