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

Magento 2.3 Admin panel blank issue

After the Installation of Magento from the composer, we cannot access the admin panel. Also, the deploy command doesn't work. (On Windows 10 OS). Note: Magento 2.3 supports Linux OS. Windows and Mac OS is not supported. This is a Magento bug. Wrong paths to Windows are generated. The fixed fix is Magento 2.3.0 - 2.3.3 #/vendor/magento/framework/View/Element/Template/File/Validator.php:140 the string if (0 === strpos($realPath, $directory)) {     return true; } to replace $realDirectory = $this->fileDriver->getRealPath($directory); if (0 === strpos($realPath, $realDirectory)) {    return true; } Magento 2.2.7 /vendor/magento/framework/View/Element/Template/File/Validator.php:113 code protected function isPathInDirectories($path, $directories) {     if (!is_array($directories)) {         $directories = (array)$directories;     }     foreach ($directories as $directory) {   ...

Magento webservice

Magento provides webservice with the ability to manage your eCommerce stores by providing calls for working with resources such as customers, categories, products, and sales orders. It also allows you to manage shopping carts and inventory. A SOAP v2 API version has been available since Magento 1.3, and a WS-I compliant version has been available since Magento 1.6. The Magento API supports SOAP and XML-RPC, where SOAP is the default protocol. With SOAP api, To connect to Magento SOAP web services, load the WSDL into your SOAP client from either of these URLs: http://hostname/api/soap/?wsdl where hostname  is the domain for your Magento host As of v1.3, you may also use the following URL to access the Magento API v2, which has been added to improve compatibility with Java and .NET: http://magentohost/api/v2_soap?wsdl=1 The following PHP example shows how to make SOAP calls to the Magento API v1: require_once('app/Mage.php'); Mage::app(); try { $client = n...

Cyber Security

Cybersecurity is the practice of protecting systems, networks, and programs from digital attacks. These cyberattacks are usually aimed at accessing, changing, or destroying sensitive information; extorting money from users; or interrupting normal business processes. Implementing effective cybersecurity measures is particularly challenging today because there are more devices than people, and attackers are becoming more innovative. What is cybersecurity all about? A successful cybersecurity approach has multiple layers of protection spread across the computers, networks, programs, or data that one intends to keep safe. In an organization, the people, processes, and technology must all complement one another to create an effective defense from cyber attacks. A unified threat management system can automate integrations across select Cisco Security products and accelerate key security operations functions: detection, investigation, and remediation. People Users must understand ...