Top sellers

Specials

Main >> PrestaShop Tips and Modules

Removing IDs from Product Page URLs in Prestashop

A common topic you see in Prestashop forum is the request to remove id numbers from friendly URLs mostly for SEO purposes. There is hardly any benefit to your search engine ranking with such a change, if any. Having the product name is more than sufficient for any SEO purpose and any "expert" who advises you otherwise clearly does not know much about search engines. Google does advise you to put product names and such in the page url, but the ids are also expected and accounted for. Having those ids have no affect on your ranking. This would even have a negative effect if you have been running your site for a while and the urls are already indexed. The search engines will identify the new links as new pages and they will be re-indexed which takes time and you will get duplicate content and 404 warnings for a while.

But most people still like to remove them and have nicer URLs. In this article I will explain how you can remove the ids from the product pages. This article does not cover the other pages yet, because the htaccess rules become more complicated as you remove ids.

First thing is you need to change the .htaccess file to accomodate. There are four lines in the .htaccess file that refers to the product pages:


RewriteRule ^lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$3&isolang=$1$5 [L,E]
RewriteRule ^lang-([a-z]{2})/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$2&isolang=$1$4 [L,E]
.....
RewriteRule ^([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$2$4 [L,E]
RewriteRule ^([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$1$3 [L,E]

These should be changed to the following:


RewriteRule ^lang-([a-z]{2})/([a-zA-Z0-9-]*)/([a-zA-Z0-9-]*).html(.*)$ /product.php?product=$3&isolang=$1$4 [L,E]
RewriteRule ^lang-([a-z]{2})/([a-zA-Z0-9-]*).html(.*)$ /product.php?product=$2&isolang=$1$3 [L,E]
.....
RewriteRule ^([a-zA-Z0-9-]*)/([a-zA-Z0-9-]*).html(.*)$ /product.php?product=$2$3 [L,E]
RewriteRule ^([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?product=$1$2 [L,E]

Once this is done you need to add the following lines to the product.php file to use the link rewrite instead of the product id. Add the following to the product.php file after line 5:

 


if(Tools::getValue('product')){
    $_GET['id_product']=Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product_lang WHERE link_rewrite="'.pSQL(Tools::getValue('product')).'"');
}

This should be added after init.php file is included. The last thing is to modify the classes/Link.php file to use the new links without the IDs.

In the classes/Link.php file look for the function "getProductLink" (just search it in your text editor). Change the function with the following (taken from 1.3):

 


    public function getProductLink($id_product, $alias = NULL, $category = NULL, $ean13 = NULL){
        global $cookie;
        $lang_link = "";
        if (!isset($this->allow)) $this->allow = 0;
        if ($this->allow && $cookie->id_lang != Configuration::get('PS_LANG_DEFAULT'))
            $lang_link = "lang-".Language::getIsoById($cookie->id_lang)."/";
        if (is_object($id_product))
            return ($this->allow == 1)?(_PS_BASE_URL_.__PS_BASE_URI__.$lang_link.(($id_product->category != 'home' AND !empty($id_product->category)) ? $id_product->category.'/' : '').$id_product->link_rewrite.($id_product->ean13 ? '-'.$id_product->ean13 : '').'.html') :
            (_PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.intval($id_product->id));
        elseif ($alias)
            return ($this->allow == 1)?(_PS_BASE_URL_.__PS_BASE_URI__.$lang_link.
(($category AND $category != 'home') ? ($category.'/') : '').$alias.($ean13 ? '-'.$ean13 : '').'.html') :  (_PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.intval($id_product));
        else
            return _PS_BASE_URL_.__PS_BASE_URI__.'product.php?id_product='.intval($id_product);
   }

With these you should have your product pages working without the IDs. Try it on a local copy before going live.

For more search engine optimizations check my canonical seo module. It let's you reduce duplicate content errors considerably.

Author: Burhan BAVKIR



Share |

Burhan BAVKIR

I have released a module that does this automatically in 1.4.

http://www.bvkyazilim.com/cart/prestashop-modules/41-prestashop-pretty-url.html


HomeHome