Skip to main content

OpenCart - How to change url to seo friendly


If we want to make the default url of opencart to seo friendly, we need to modify seo_url.php controller.

Firstly, backup your file. Then, in seo_url.php controller, replace the code with below:

1. edit controller/common/seo_url.php
<?php
class ControllerCommonSeoUrl extends Controller {
   public function index() {
      // Add rewrite to url class
      if ($this->config->get('config_seo_url')) {
         $this->url->addRewrite($this);
      }
   
      // Decode URL
      if (isset($this->request->get['_route_'])) {
         $parts = explode('/', $this->request->get['_route_']);

         $route = "";
       
         foreach ($parts as $part) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
         
            if ($query->num_rows) {
               $url = explode('=', $query->row['query']);

               if(count($url) > 1){
             
               if ($url[0] == 'product_id') {
                  $this->request->get['product_id'] = $url[1];
               }
             
               if ($url[0] == 'category_id') {
                  if (!isset($this->request->get['path'])) {
                     $this->request->get['path'] = $url[1];
                  } else {
                     $this->request->get['path'] .= '_' . $url[1];
                  }
               }
             
               if ($url[0] == 'manufacturer_id') {
                  $this->request->get['manufacturer_id'] = $url[1];
               }
             
               if ($url[0] == 'information_id') {
                  $this->request->get['information_id'] = $url[1];
               }
               }else{
                  $route = $url[0];
               }
            } else {
               $this->request->get['route'] = 'error/not_found';
            }
         }
       
         if (isset($this->request->get['product_id'])) {
            $this->request->get['route'] = 'product/product';
         } elseif (isset($this->request->get['path'])) {
            $this->request->get['route'] = 'product/category';
         } elseif (isset($this->request->get['manufacturer_id'])) {
            $this->request->get['route'] = 'product/manufacturer/product';
         } elseif (isset($this->request->get['information_id'])) {
            $this->request->get['route'] = 'information/information';
         }else {
            $this->request->get['route'] = $route;
         }

       
         if (isset($this->request->get['route'])) {
            return $this->forward($this->request->get['route']);
         }
      }
   }
 
   public function rewrite($link) {
      if ($this->config->get('config_seo_url')) {
         $url_data = parse_url(str_replace('&amp;', '&', $link));
   
         $url = '';
       
         $data = array();
   
         parse_str($url_data['query'], $data);
       
         foreach ($data as $key => $value) {
            if (($key == 'product_id') || ($key == 'manufacturer_id') || ($key == 'information_id')) {
               $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
         
               if ($query->num_rows) {
                  $url .= '/' . $query->row['keyword'];
               
                  unset($data[$key]);
               }            
            } elseif ($key == 'path') {
               $categories = explode('_', $value);
             
               foreach ($categories as $category) {
                  $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = 'category_id=" . (int)$category . "'");
         
                  if ($query->num_rows) {
                     $url .= '/' . $query->row['keyword'];
                  }                  
               }
             
               unset($data[$key]);
            }elseif ($key == 'route') {
               $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($value) . "'");
         
               if ($query->num_rows) {
                  $url .= '/' . $query->row['keyword'];
               
                  unset($data[$key]);
               }            
            }
         }
   
         if ($url) {
            unset($data['route']);
       
            $query = '';
       
            if ($data) {
               foreach ($data as $key => $value) {
                  $query .= '&' . $key . '=' . $value;
               }
             
               if ($query) {
                  $query = '?' . trim($query, '&');
               }
            }

            return $url_data['scheme'] . '://' . $url_data['host'] . (isset($url_data['port']) ? ':' . $url_data['port'] : '') . str_replace('/index.php', '', $url_data['path']) . $url . $query;
         } else {
            return $link;
         }
      } else {
         return $link;
      }    
   }
}
?>

2. Open PHPMyadmin and goto url_alias table and insert the following:

INSERT INTO url_alias (query, keyword) VALUES ('common/home', '');
INSERT INTO url_alias (query, keyword) VALUES ('account/wishlist', 'wishlist');
INSERT INTO url_alias (query, keyword) VALUES ('account/account', 'my-account');
INSERT INTO url_alias (query, keyword) VALUES ('checkout/cart', 'shopping-cart');
INSERT INTO url_alias (query, keyword) VALUES ('checkout/checkout', 'checkout');
INSERT INTO url_alias (query, keyword) VALUES ('account/login', 'login');
INSERT INTO url_alias (query, keyword) VALUES ('account/logout', 'logout');
INSERT INTO url_alias (query, keyword) VALUES ('account/order', 'order-history');
INSERT INTO url_alias (query, keyword) VALUES ('account/newsletter', 'newsletter');
INSERT INTO url_alias (query, keyword) VALUES ('product/special', 'specials');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/account', 'affiliates');
INSERT INTO url_alias (query, keyword) VALUES ('checkout/voucher', 'gift-vouchers');
INSERT INTO url_alias (query, keyword) VALUES ('product/manufacturer', 'brands');
INSERT INTO url_alias (query, keyword) VALUES ('information/contact', 'contact-us');
INSERT INTO url_alias (query, keyword) VALUES ('account/return/insert', 'request-return');
INSERT INTO url_alias (query, keyword) VALUES ('information/sitemap', 'sitemap');
INSERT INTO url_alias (query, keyword) VALUES ('account/forgotten', 'forgot-password');
INSERT INTO url_alias (query, keyword) VALUES ('account/download', 'downloads');
INSERT INTO url_alias (query, keyword) VALUES ('account/return', 'returns');
INSERT INTO url_alias (query, keyword) VALUES ('account/transaction', 'transactions');
INSERT INTO url_alias (query, keyword) VALUES ('account/register', 'create-account');
INSERT INTO url_alias (query, keyword) VALUES ('product/compare', 'compare-products');
INSERT INTO url_alias (query, keyword) VALUES ('product/search', 'search');
INSERT INTO url_alias (query, keyword) VALUES ('account/edit', 'edit-account');
INSERT INTO url_alias (query, keyword) VALUES ('account/password', 'change-password');
INSERT INTO url_alias (query, keyword) VALUES ('account/address', 'address-book');
INSERT INTO url_alias (query, keyword) VALUES ('account/reward', 'reward-points');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/edit', 'edit-affiliate-account');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/password', 'change-affiliate-password');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/payment', 'affiliate-payment-options');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/tracking', 'affiliate-tracking-code');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/transaction', 'affiliate-transactions');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/logout', 'affiliate-logout');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/forgotten', 'affiliate-forgot-password');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/register', 'create-affiliate-account');
INSERT INTO url_alias (query, keyword) VALUES ('affiliate/login', 'affiliate-login');




Comments

Popular posts from this blog

Error on payment method when placing order "No Such Entity With Cart ID"

No Such Entity With Cart ID Error on payment method when placing order. If you receive the error message " No such entity. ", " No such entity with " or "No such entity with  customerId,OrderId " in Magento 2, the issue usually occurred when you try to load not existing object via Magento 2 Repository Class. To debug this issue, please open the file vendor/magento/framework/Exception/NoSuchEntityException.php and at the beginning of the  __construct  method temporary add debug backtrace code: foreach ( debug_backtrace () as $_stack ) { echo ( $_stack [ "file" ] ? $_stack [ "file" ] : '' ) . ':' . ( $_stack [ "line" ] ? $_stack [ "line" ] : '' ) . ' - ' . ( $_stack [ "function" ] ? $_stack [ "function" ] : '' ); } exit (); example: public function __construct ( Phrase $phrase = null , \...

Prestashop Add canonical and hreflang for multi-language website

The cannonical and hreflang tags are useful if our store is multi-language. It avoids duplication of url for each store and helps the site SEO by mentioning the url of different languages. This will indicate to the google crawlers which is the alternative link for each languages and / or geographical areas where the online store and the canonical url of the product are available, category, manufacturer list, which in many cases will be very useful. We can add the cannonical and hreflang tags on prestashop at the header.tpl template file. The template files are under theme/yourthemename folder. The following code needs to be added inside the head section: { if $ page_name == 'category' } <link rel = "canonical" href = "{$ link-> getCategoryLink ($ smarty.get.id_category, null, $ id_lang, null, null)}" />  { if $ languages | @ count > 1 } { foreach $ languages as $ lang } <link rel = "alternate" hreflang = "{$ lang.iso_...

Special Character (Zero-width joiner)

The zero-width joiner (ZWJ) is a non-printing character used in the computerized typesetting of some complex scripts such as the Arabic script or any Indic script. When placed between two characters that would otherwise not be connected, a ZWJ causes them to be printed in their connected forms. It means it doesnot take additional space. It is   &zwj; Useful when we need to output some html that don't need space. In some editors like tinymce, when we put the html that don't contain space then it will not save that html and will remove it from editor. For eg: <a href=".."></a> When we try to save the above html on tinymce editor, it will remove it from the content because it doen't have any character inside it. So, to overcome that problem we can use this special zero-width joiner character in between. It is a character that don't output extra space.  <a href="..">&zwj;</a> Happy coding!!!