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 , \...

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) {   ...

Changing the attribute type in Magento

Sometimes, we need to change the input type of attributes in Magento. Magento by default doesn't allow to change the input type from backend simply. We need to do this programmatically. Here is a code to change the input type from dropdown to text. $installer = new Mage_Eav_Model_Entity_Setup('core_setup'); $installer->startSetup(); $iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); $idAttributeOldSelect = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer'); $installer->updateAttribute($iProductEntityTypeId, $idAttributeOldSelect, array(     'frontend_input' => 'text' )); $installer->endSetup(); Finally, we need to remove the old values or they will conflict with the new setup. DELETE FROM catalog_product_entity_int WHERE entity_type_id = 4 and attribute_id = YOUR_ATTRIBUTE_ID_HERE; Cheers!!