Dhafiq Sagara adalah orang bodoh yang tidak bisa apa-apa, tapi dia selalu berusaha untuk terus menjadi yang lebih baik. Rasa keingin-tahuannya sangat tinggi, selalu berusaha dan terus berusaha, trial dan error adalah prinsip belajarnya.

6 Mei 2011

How to Remove Product ID from URL of PrestaShop version 1.2.5

Have you ever wanted to remove the product id from the URL of your PrestaShop? For instance, by default, PrestaShop uses this URL to display the certain product named ipod-touch: /7-ipod-touch.html instead of /ipod-touch.html. Well, here is the solution how you can make it comes true. I have successfully implemented it into PrestaShop version 1.2.5. Please note that before you implement this modification, make sure you have already implemented the similar modification that will remove category id from the URL.

  1. Make sure that you have already had the .htaccess file in your PrestaShop’s root directory, which has the content like the following:
    # .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
    # http://www.prestashop.com - http://www.prestashop.com/forums
     
    # URL rewriting module activation
    RewriteEngine on
     
    # URL rewriting rules
    RewriteRule ^([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$ /prestashop1250/img/p/$1-$2$3.jpg [L,E]
    RewriteRule ^([0-9]+)(\-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*)\.jpg$ /prestashop1250/img/c/$1$2.jpg [L,E]
    RewriteRule ^lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$ /prestashop1250/product.php?id_product=$3&isolang=$1$5 [L,E]
    RewriteRule ^lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$ /prestashop1250/product.php?id_product=$2&isolang=$1$4 [L,E]
    RewriteRule ^lang-([a-z]{2})/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$ /prestashop1250/category.php?id_category=$2&isolang=$1 [QSA,L,E]
    RewriteRule ^([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$ /prestashop1250/product.php?id_product=$2$4 [L,E]
    RewriteRule ^([0-9]+)\-([a-zA-Z0-9-]*)\.html(.*)$ /prestashop1250/product.php?id_product=$1$3 [L,E]
    RewriteRule ^([0-9]+)\-([a-zA-Z0-9-]*)(.*)$ /prestashop1250/category.php?id_category=$1 [QSA,L,E]
    RewriteRule ^content/([0-9]+)\-([a-zA-Z0-9-]*)(.*)$ /prestashop1250/cms.php?id_cms=$1 [QSA,L,E]
    RewriteRule ^([0-9]+)__([a-zA-Z0-9-]*)(.*)$ /prestashop1250/supplier.php?id_supplier=$1$3 [QSA,L,E]
    RewriteRule ^([0-9]+)_([a-zA-Z0-9-]*)(.*)$ /prestashop1250/manufacturer.php?id_manufacturer=$1$3 [QSA,L,E]
    RewriteRule ^lang-([a-z]{2})/(.*)$ /prestashop1250/$2?isolang=$1 [QSA,L,E]
     
    # Catch 404 errors
    ErrorDocument 404 /prestashop1250/404.php
     
    # Remove Category ID from URL. You have to modify also the /classes/Link.php file! Modified by Masino Sinaga, May 3, 2010
    RewriteRule ^accessories-ipod$ /prestashop1250/category.php?id_category=3 [QSA,L,E]
    RewriteRule ^music-ipods$ /prestashop1250/category.php?id_category=2 [QSA,L,E]
    RewriteRule ^laptops$ /prestashop1250/category.php?id_category=1 [QSA,L,E]
    That .htaccess file is generated from the link I mentioned above about. You can generate this .htaccess file from admin panel at Tools tab, then choose Generators sub-tab, and follow the instructions carefully there. In this example above, I use prestashop1250 for the domain name. Please adjust it with yours.
  2. Add this following code after the last line of that code above:
    # Remove Product ID from URL. You have to modify also the /classes/Link.php file! Modified by Masino Sinaga, May 4, 2010
    RewriteRule ^music-ipods/ipod-nano\.html(.*)$ product.php?id_product=1$1 [QSA,L,E]
    RewriteRule ^music-ipods/ipod-shuffle\.html(.*)$ product.php?id_product=2$1 [QSA,L,E]
    RewriteRule ^laptops/macbook-air\.html(.*)$ product.php?id_product=5$1 [QSA,L,E]
    RewriteRule ^laptops/macbook\.html(.*)$ product.php?id_product=6$1 [QSA,L,E]
    RewriteRule ^music-ipods/ipod-touch\.html(.*)$ product.php?id_product=7$1 [QSA,L,E]
    RewriteRule ^accessories-ipod/housse-portefeuille-en-cuir-belkin-pour-ipod-nano-noir-chocolat\.html(.*)$ product.php?id_product=8$1 [QSA,L,E]
    RewriteRule ^accessories-ipod/ecouteurs-a-isolation-sonore-shure-se210-blanc\.html(.*)$ product.php?id_product=9$1 [QSA,L,E]
    It meant that we will adjust the 7 products name: ipod-nano, ipod-shuffle, macbook-air, macbook, ipod-touch, housse-portefeuille-en-cuir-belkin-pour-ipod-nano-noir-chocolat, and ecouteurs-a-isolation-sonore-shure-se210-blanc. In other words, if you have more than 7 products above, you have to define or add your own product one by one into the .htaccess file.
  3. Open your /classes/Link.php file, find this code:
    public function getProductLink($id_product, $alias = NULL, $category = NULL, $ean13 = NULL)
    {
    if (!isset($this->allow)) $this->allow = 0;
    if (is_object($id_product))
    return ($this->allow == 1)?(_PS_BASE_URL_.__PS_BASE_URI__.(($id_product->category != 'home' AND !empty($id_product->category)) ? $id_product->category .'/' : '').intval($id_product->id).'-'.$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__.(($category AND $category != 'home') ? ($category.'/') : '').intval($id_product).'-'.$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);
    }
    
    then replace with this following code:
    public function getProductLink($id_product, $alias = NULL, $category = NULL, $ean13 = NULL)
    {
    if (!isset($this->allow)) $this->allow = 0;
    if (is_object($id_product))
    return ($this->allow == 1)?(_PS_BASE_URL_.__PS_BASE_URI__.(($id_product->category != 'home' AND !empty($id_product->category)) ? $id_product->category .'/' : '')./*intval($id_product->id).'-'.*/$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__.(($category AND $category != 'home') ? ($category.'/') : '')./*intval($id_product).'-'.*/$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);
    }
    

Now you should have the URL without the category and product id in it.

Tidak ada komentar:

Posting Komentar