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 Category ID from URL of PrestaShop version 1.2.5

This following modification will remove the category id from the URL of your PrestaShop. As we know, by default, PrestaShop uses the URL for category such as: /3-accessories-ipod instead of accessories-ipod. Please note that you have to enable the URL Friendly feature that you can set it up from the Preferences tab in admin panel by choosing the Yes option in order to implement this modification. I have successfully applied this modification into PrestaShop version 1.2.5 (the stable version until I wrote this current article).

  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
    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 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]
    It meant that we will adjust the 3 categories name: accessories-ipod, music-ipods, and laptops. In other words, if you have more than 3 categories above, you have to define or add your own category one by one into the .htaccess file.
  3. Open your /classes/Link.php file, and find this code:
    public function getCategoryLink($id_category, $alias = NULL)
    {
    if (is_object($id_category))
    return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__.intval($id_category->id).'-'.$id_category->link_rewrite) : 
    (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category->id));
    if ($alias)
    return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__.intval($id_category).'-'.$alias) :
    (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category));
    return _PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category);
    }
    then replace with this following code:
    public function getCategoryLink($id_category, $alias = NULL)
    {
    if (is_object($id_category))
    return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__./*intval($id_category->id).'-'.*/$id_category->link_rewrite) : 
    (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category->id));
    if ($alias)
    return ($this->allow == 1) ? (_PS_BASE_URL_.__PS_BASE_URI__./*intval($id_category).'-'.*/$alias) :
    (_PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category));
    return _PS_BASE_URL_.__PS_BASE_URI__.'category.php?id_category='.intval($id_category);
    }

Now you should have the URL without the category id. In the next article I will try to modify for the others (cms id, product id, etc).

Tidak ada komentar:

Posting Komentar