Jump to content

How to redirect from your old URLs to new prestashop URLS


phunter121

Recommended Posts

I thought some of you might be interested in this. I have this working for product pages in a test environment, so would welcome thoughts.

My website has about 3000 products and I had the problem that my old website has very well estabilshed URLs that are highly ranked by Google as well as other websites that link to us. So i needed a method of redirecting spiders and customers to the new URLs without having 3000+ rewrite rules within my .htaccess file.

For example, I needed to redirect:
http://www.canineconcepts.co.uk/item--The-Outward-Hound-Courier-Style-Pet-Carrier--dog-carrier---dogs
to:
http://www.canineconcepts.co.uk/prestashop1-4/en/517-the-outward-hound-courier-style-pet-carrier.html

In this example, 'dog-carrier' is the product_id in my old shop and 517 is the new presta id!!!

Here is what I have done:

1. I copied the rewrite rule in my old .htaccess file to my new presta one and then amended it. This is an example:
ORIGINAL
RewriteRule ^item--(.*)--(.*)---(.*) /cgi-bin/ccp51/cp-app.cgi?seo=item--$1&pet;=$2
AMENDED TO
RewriteRule ^item--(.*)--(.*)---(.*) /prestashop1-4/product.php?ccpid=$2 [QSA,L]

Having done the above, any product links in the old format, will now arrive into my prestashop.

2. Amend controllers/productcontroller.php around line 98, just below code'global $cart;'
The following code traps these page requests in the old format, finds the product in presta and redirects to the new URL. To do this, in my case, as part of my upload of products into Presta, I have stored the old product key in the 'supplier_reference' field as I don't plan to use this field. So this code, parses the URL to get the old product key (ccpid) and uses it to find the product in presta. Once we have this, it uses 'header' object to redirect the browser to the new URL. Since it is a 301 redirect, google spiders will permanently redirect with minimal loss of page rank.

     // PH MOD 6arp11 handles pages requests entering presta with a ccp url and following this .htaccess rewrite rule:
       // RewriteRule ^item--(.*)--(.*)---(.*) /prestashop1-4/product.php?ccpid=$2 [QSA,L]
       $ccpid = $_GET['ccpid'];
       if (isset($ccpid))
           {
           //attempt to find the product using the ccpid within the 'supply_reference' field
           $query = "SELECT id_product FROM " ._DB_PREFIX_ ."product " ."WHERE supplier_reference = '" .$_GET['ccpid'] ."'";
           $results = Db::getInstance()->executeS($query);  //execute the SQL
           foreach($results as $result)
               {
               $product_id = (int)$result['id_product'];  //store the product ID
               $product = new Product($product_id, true, intval($cookie->id_lang));  //create a new product object

               if($product->link_rewrite) {  //if the
                   $product_url =  self::$link->getProductLink($product->id, $product->link_rewrite);
                   Header( "HTTP/1.1 301 Moved Permanently" );
                   Header( "Location: ".$product_url );
                   exit();
                   }
               }
       }
       //END OF PH MOD

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...