26

How can you customize system URLs in OpenCart? For example, I would like http://example.com/index.php?route=checkout/cart to be displayed as http://example.com/cart

I know OpenCart provides SEO URLs for products, categories, manufacturers and information pages, but it doesn't look like there is anything built-in (at least prior to version 1.5.0) for anything else.

1
  • 1
    try this vqmod, i made with the second answer codes an some edits to fix a litlle bug: pastebin.com/yAsfxqTm
    – Dreanmer
    Commented Jul 23, 2014 at 14:04

8 Answers 8

27

It turns out this can be done with a relatively simple change to a single file. No .htaccess rewrite rules, simply patch the catalog/controller/common/seo_url.php file and add your pretty URLs to an existing database table.


The patch to seo_url.php:

Index: catalog/controller/common/seo_url.php
===================================================================
--- catalog/controller/common/seo_url.php   (old)
+++ catalog/controller/common/seo_url.php   (new)
@@ -48,7 +42,12 @@
                $this->request->get['route'] = 'product/manufacturer/product';
            } elseif (isset($this->request->get['information_id'])) {
                $this->request->get['route'] = 'information/information';
-           }
+           } else {
+                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
+                if ($query->num_rows) {
+                    $this->request->get['route'] = $query->row['query'];
+                }
+           }

            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
@@ -88,7 +87,15 @@
                        }

                        unset($data[$key]);
-                   }
+                   } else {
+                        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");
+
+                        if ($query->num_rows) {
+                            $url .= '/' . $query->row['keyword'];
+
+                            unset($data[$key]);
+                        }
+                   }
                }
            }

There are two edits required. The first extends the index() function to look in the url_alias table for any keyword matching $this->request->get['_route_'].

The second extends the rewrite() function to look in the url_alias table for all routes, not just those for products, manufacturers, and information pages.


Adding entries to the database:

INSERT INTO `url_alias` (`url_alias_id`, `query`, `keyword`) VALUES
(NULL, 'checkout/cart', 'cart');

That's it. http://example.com/cart should return the same thing that http://example.com/index.php?route=checkout/cart does, and OpenCart should recognize $this->url->link('checkout/cart'); and return a link to the pretty URL http://example.com/cart

5
  • This works pretty well, thanks a lot. Just the manufactures index is flawed. Commented Jan 27, 2012 at 7:53
  • as it turned out... this is hardcoded in /template/product/manufacturer_list.tpl @ line 12 Commented Jan 27, 2012 at 18:24
  • Where do you add this exactly?
    – Mr.Flocker
    Commented Jul 21, 2013 at 12:55
  • 1
    Could you please paste your entire controller file? It would make it much easier than trying to understand ^^^ the line numbers and stuff. It looks like the perfect approach, and I would appreciate it if you would paste your entire ControllerCommonSeoUrl class file. Thanks! Commented Jan 26, 2015 at 7:23
  • i am using oc 2.0.2.0. i tried your code plus added entries in database but it is not working. check pastebin.com/DVWH7sby. Is it correct way? Commented Aug 5, 2015 at 13:26
15

I'm using Opencart version 1.5.5.1 and this is the exact code that worked for me:

<?php 
class ControllerCommonSeoUrl extends Controller {
    /* SEO Custom URL */
    private $url_list = array (
        'common/home'       => '',
        'checkout/cart'     => 'cart',
        'account/register'  => 'register',
                    'account/wishlist'  => 'wishlist',
                    'checkout/checkout' => 'checkout',
                    'account/login'     => 'login',
                    'product/special'   => 'special',
                    'affiliate/account' => 'affiliate',
                    'checkout/voucher'  => 'voucher',
                    'product/manufacturer' => 'brand',
                    'account/newsletter'   => 'newsletter',
                    'account/order'        => 'order',
                    'account/account'      => 'account',
                    'information/contact'  => 'contact',
                    'account/return/insert' => 'return',
                    'information/sitemap'   => 'sitemap',
        );
    /* SEO Custom URL */

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_']);

        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 ($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 {
                $this->request->get['route'] = 'error/not_found';   
            }
        }
                    /* SEO Custom URL */
                    if ( $_s = $this->setURL($this->request->get['_route_']) ) {
                            $this->request->get['route'] = $_s;
                    }/* SEO Custom URL */

        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/info';
        } elseif (isset($this->request->get['information_id'])) {
            $this->request->get['route'] = 'information/information';
        }

        if (isset($this->request->get['route'])) {
            return $this->forward($this->request->get['route']);
        }
    }
}

public function rewrite($link) {
    $url_info = parse_url(str_replace('&amp;', '&', $link));

    $url = ''; 

    $data = array();

    parse_str($url_info['query'], $data);

    foreach ($data as $key => $value) {
        if (isset($data['route'])) {
            if (($data['route'] == 'product/product' && $key == 'product_id') || (($data['route'] == 'product/manufacturer/info' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $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]);
            }
                                    /* SEO Custom URL */
                                    if( $_u = $this->getURL($data['route']) ){
                                        $url .= $_u;
                                        unset($data[$key]);
                                    }/* SEO Custom URL */       


        }
    }

    if ($url) {
        unset($data['route']);

        $query = '';

        if ($data) {
            foreach ($data as $key => $value) {
                $query .= '&' . $key . '=' . $value;
            }

            if ($query) {
                $query = '?' . trim($query, '&');
            }
        }

        return $url_info['scheme'] . '://' . $url_info['host'] . (isset($url_info['port']) ? ':' . $url_info['port'] : '') . str_replace('/index.php', '', $url_info['path']) . $url . $query;
    } else {
        return $link;
    }
}   
    /* SEO Custom URL */
    public function getURL($route) {
            if( count($this->url_list) > 0) {
                 foreach ($this->url_list as $key => $value) {
                    if($route == $key) {
                        return '/'.$value;
                    }
                 }
            }
            return false;
    }
    public function setURL($_route) {
            if( count($this->url_list) > 0 ){
                 foreach ($this->url_list as $key => $value) {
                    if($_route == $value) {
                        return $key;
                    }
                 }
            }
            return false;
    }/* SEO Custom URL */
}
?>
4
  • i added this code in the seo_url.php file in my controller/common folder. i am running 1.5.5.1. i would like that in the url or a product i will see both the category & subcategory along with the item itself
    – Vad.Gut
    Commented May 15, 2013 at 8:50
  • This is awesome, no idea why they don't include this by default. It's a bit ridiculous. Commented Nov 7, 2013 at 22:00
  • this is awesome, but have some problems. because you rewrite the url, you can get into a situation you have no query string parameters, then you need to check is $url_info['query'] exists before use it, then you need to check too if $data['route'] exists. But i still with a problem with some links, like remove from cart link... i maked a vqmod version of this pastebin.com/yAsfxqTm
    – Dreanmer
    Commented Jul 22, 2014 at 19:51
  • above code is successfully converting the website into SEO friendly but the events are not working I am working on this example: catalog/controller/account/account/after Commented Nov 27, 2017 at 11:02
2

A simple "no code" method is to install vQmod. Instructions here: vQmod install wiki

And then upload the xml file available in this Opencart forum thread to the vqmod/xml/ folder.

The xml contains a relatively easy to read script that maps the urls similar to the two answers above, but without modifying core files. So site updates will not kill it.

1

Put this near the top of your index.php file. It was the only solution that worked for me.

switch($_SERVER["REQUEST_URI"])
{
    case '/old-url': $three01 = "/new-url"; break;
}

if($three01)
{
    header("HTTP/1.1 301 Moved Permanently"); header("Location: ".$three01); exit;
}
1

vQmod xml file

<modification>
<id>Seo All Alias</id>
<version>1.0</version>
<vqmver>2.1.7</vqmver>
<author>noname</author>
<file name="catalog/controller/common/seo_url.php"> 
    <operation>
        <search position="after" offset="2"><![CDATA[
            $this->request->get['route'] = 'information/information';
        ]]></search>
        <add><![CDATA[
        else {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($this->request->get['_route_']) . "'");
            if ($query->num_rows) {
                $this->request->get['route'] = $query->row['query'];
            }
        }
        ]]></add>
    </operation>

    <operation>
        <search position="after" offset="1" index="2"><![CDATA[
            unset($data[$key]);
        ]]></search>
        <add><![CDATA[
                else {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($data['route']) . "'");

                    if ($query->num_rows) {
                        $url .= '/' . $query->row['keyword'];
                        unset($data[$key]);
                    }
                }
        ]]></add>
    </operation>

</file>

1
  • This worked for me - after manually adding the new seo rewrite to my database _url_aliases. This is easier than modifying the file itself. I am using 2.3.0.2 Commented Jul 13, 2017 at 2:47
0

OpenCart SEO for OpenCart 1.5.X free :)

You can use as you wish. Category Name / ID / ID for quicker SEO resolving

<?php
class ControllerCommonSeoUrl extends Controller {
        /* SEO Custom URL */
        private $url_list = array (
            'common/home'       => 'home',
            'checkout/cart'     => 'cart',
            'account/register'  => 'register',
                        'account/wishlist'  => 'wishlist',
                        'checkout/checkout' => 'checkout',
                        'account/login'     => 'login',
                        'product/special'   => 'special',
                        'affiliate/account' => 'affiliate',
                        'checkout/voucher'  => 'voucher',
                        'product/manufacturer' => 'brand',
                        'account/newsletter'   => 'newsletter',
                        'account/order'        => 'order',
                        'account/account'      => 'account',
                        'information/contact'  => 'contact',
                        'account/return/insert' => 'return/insert',
                        'information/sitemap'   => 'sitemap',
            );
        /* SEO Custom URL */

        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_']);

                                                if ( count($parts) > 1 ) {
                                                        if ($parts[1] == 'category'){
                                                                $this->request->get['path'] = $parts[2];
                                                                for ( $i = 3 ; $i < count($parts); $i++) {      
                                                                        $this->request->get['path'] .= '_' . $parts[$i];
                                                                }
                                                        }elseif( $parts[1] == 'item' ) {
                                                                $this->request->get['product_id'] = $parts[2];
                                                        }
                                                }
                        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 ($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 {
                                        $this->request->get['route'] = 'error/not_found';       
                                }
                        }
                        /* SEO Custom URL */
                        if ( $_s = $this->setURL($this->request->get['_route_']) ) {
                                $this->request->get['route'] = $_s;
                        }/* SEO Custom URL */

                        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';
                        }

                        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 (isset($data['route'])) {
                                        if ( (($data['route'] == 'product/manufacturer/product' || $data['route'] == 'product/product') && $key == 'manufacturer_id') || ($data['route'] == 'information/information' && $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 == 'product_id' ) {

                                                $url = '/shop/item/'.$value;
                                                unset($data[$key]);

                                              }elseif ($key == 'path') {

                                                $categories = explode('_', $value);
                                                $url = '/shop/category';
                                                foreach ($categories as $category) {
                                                        $url .= '/'.$category;
                                                }
                                                unset($data[$key]);

                                        }// 
                                        /* SEO Custom URL */
                                        if( $_u = $this->getURL($data['route']) ){
                                            $url .= $_u;
                                            unset($data[$key]);
                                        }/* SEO Custom URL */        


                                }
                        }

                        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;
                }               
        }

        /* SEO Custom URL */
        public function getURL($route) {
                if( count($this->url_list) > 0) {
                     foreach ($this->url_list as $key => $value) {
                        if($route == $key) {
                            return '/'.$value;
                        }
                     }
                }
                return false;
        }
        public function setURL($_route) {
                if( count($this->url_list) > 0 ){
                     foreach ($this->url_list as $key => $value) {
                        if($_route == $value) {
                            return $key;
                        }
                     }
                }
                return false;
        }/* SEO Custom URL */
}
?>
1
  • 1
    duplicate, less info.
    – Dreanmer
    Commented Jul 22, 2014 at 11:55
0

Depending on the level of customization you need, there are a few extensions available, including free ones.

This one works both on OpenCart 1.5 and OpenCart 2 and seems quite customizable. However it does not support customization of hardcoded URL parts like "checkout/cart" or "account/wishlist".

Here is a vQmod for OpenCart 2.0 (might work with 1.5, not tested) which applies to the forementioned extension and allows to change these hardcoded strings into whatever you want, and support multilanguage websites.

E.g. "checkout/cart" will become "cart" for English, "panier" for French, etc.

-1

here is a free Extension that adds custom SEO URLs, as well as lets you manage multi-langauge URLs, 404s, export/import, and bulk edit.

SEO Module URL

Hey, before you click down, check the extension. It is my extension, I know that it is coded properly and it does exactly what the user asks for and much more and it is FREE.

I really hope it helps someone.

Not the answer you're looking for? Browse other questions tagged or ask your own question.