OpenCart: Clean SEO URL’s for Contact Us, Site Map and other custom pages

There is a mobile optimized version of this page, view AMP Version.

Over the last few weeks I have been working with OpenCart, a free Open-Source Shopping Cart. And so far I have been loving it!

However it was not long until I hit a road block with the software, after enabling the clean SEO URL’s feature and setting up my “SEO Keywords” for all my products, categories and information pages. I soon realised there was no options to set this up for the Contact Us, Sitemap and various other pages!!

I soon figured it out though, and I will explain how to do this for the contact us and site map pages below:

First modify /catalog/controller/common/seo_url.php

add:

//custom SEO url's
if ($url[0] == 'contact') {
    $this->request->get['route'] = 'information/contact';
}   
if ($url[0] == 'sitemap') {
    $this->request->get['route'] = 'information/sitemap';
}

 

after:

if ($url[0] == 'information_id') {
    $this->request->get['information_id'] = $url[1];
}

 

Now you need to insert two URL aliases into your url_alias table with the following values:

 

Column: Value: 
 query contact=contact
 keyword contact
 query sitemap=sitemap
 keyword sitemap

 

Congratulations! It is that simple! You can now link to http://yoursite.com/contact or http://yoursite.com/sitemap.

All you need todo now is repeat the steps above to create URL aliases for any page in open cart.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

Post Tags:
, , , , , ,

Comments For This Post:

  • Thanks for this you're a boss!

  • Thanks for this, it's exactly what I'm looking for...

    although I've got a slight problem with it - it works fine in that I can use mysite.com/contact to reach the contact page but the system generated urls such as in the footer and in the breadcrumbs are still of the form:

    mysite.com/index.php?route=information/contact

    just wondered if you had a fix for this?

  • Just a quick update to my last comment,

    adding:

    } elseif (isset($data['route']) && $data['route'] == 'information/contact') {
    $url .= '/contact';

    just before the line:

    } elseif ($key == 'path') {
    $categories = explode('_', $value);

    In /catalog/controller/common/seo_url.php@rewrite() seems to do the trick

  • Nice write up. Could you, perhaps, go into a little explanation on how the URL relates to the controller/table mapping?

    For example, if I wanted the URL to be "/contact-us" instead of "/contact", what bits in the seo_url.php and url_alias table would get changed?

    Cheers!

  • Thanks Fozzy,
    You simply set "keyword" to the URL you wish to use.

    Hope this helps

  • Great, thanks for posting this! Glad you sorted things out!!

  • Hi there, firstly thank you for posting this. Unfortunately am having some troubles with it and is not working for me. Maybe you could help me out?

    i added this part:
    if ($url[0] == 'contact') {
    $this->request->get['route'] = 'information/contact';
    }
    if ($url[0] == 'sitemap') {
    $this->request->get['route'] = 'information/sitemap';
    }

    but not this as it was already in there:
    if ($url[0] == 'information_id') {
    $this->request->get['information_id'] = $url[1];
    } - See more at: http://deano.me/2013/07/opencart-clean-seo-urls-for-contact-us-site-map-and-other-custom-pages/#sthash.orsgovZ9.dpuf

    so this part still is in the file.

    Finally i went to my db on table:
    url_alias and added:
    sitemap=sitemap as query and keyword as sitemap.

    But still no success :/
    the rest build in SEOs work though.

    Any advice perhaps?
    Cheers
    Alex

  • By the way, this is how it looks in my file in general,:

    if ($url[0] == 'manufacturer_id') {
    $this->request->get['manufacturer_id'] = $url[1];
    }

    if ($url[0] == 'information_id') {
    $this->request->get['information_id'] = $url[1];
    }
    if ($url[0] == 'contact') {
    $this->request->get['route'] = 'information/contact';
    }
    if ($url[0] == 'sitemap') {
    $this->request->get['route'] = 'information/sitemap';
    }

  • Hello Deano

    I have followed your instructions as described above. But on my side the custom urls doesn't work. And if add a new information text in opencart are they just normal text. E.g. i want to add "my account like this but with a nice url: http://matrix.nicolette.ro/index.php?route=account/account. And if I set "account" in opencart/Catalog/Information then appears just a normal text?! How is it possible to change from .../index.php?route=account/account to .../myaccount?

    Here you can see my code [seo_url.php] :

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

    //custom SEO url's
    if ($url[0] == 'contact') {
    $this->request->get['route'] = 'information/contact';

    }
    if ($url[0] == 'sitemap') {
    $this->request->get['route'] = 'information/sitemap';
    }
    if ($url[0] == 'account') {
    $this->request->get['route'] = 'account/account';
    }

    if ($url[0] == 'order_list') {
    $this->request->get['route'] = 'account/order';
    }

    if ($url[0] == 'wishlist') {
    $this->request->get['route'] = 'account/wishlist';
    }

    } 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/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('&', '&', $link));

    $url = '';

    $data = array();

    if (isset($url_info['query'])) 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]);
    }
    }
    }

    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 {
    $link = str_replace('index.php?route=', '', $link);
    return $link;
    }
    }
    }
    ?>

    kind regards
    - robin

  • Excellent post, but just wondering where I edit the url_alias table? Am very new to OC and still finding my feet.
    Many thanks !!

  • Hi thnx for sharing

    it works gud when i type mystore.com/contact to show the contact page but urls such as in the footer and in the breadcrumbs are still of the form:

    mystore.com/index.php?route=information/contact

    mystore.com/contact -- i want this type when i click

  • Hey Deano, plz help me to create sort url, what you have write not working in OC 1.5.6.1

  • I have emailed you, hope to hear from you shortly.

  • I have emailed you, hope to hear back from you shortly.

  • I have emailed you, hope to hear from you shortly

  • I have emailed you, hope to hear form you soon.

  • Hi there,

    I'm able to follow everything except the location of url_alias table. I would appreciate any help. Thanks!

  • You need to insert 4 rows into your url_alias table through PHPMyAdmin... If you need any more help let me know 🙂

  • Great job mate! You saved my day! 🙂

  • Hi Mr.Daeno,

    am having some troubles with it and is not working for me.
    still have mystore.com/index.php?route=information/contact

    but i want like this: mystore.com/contact-us

  • Hey David,
    I could offer assistance if you are willing to pay for my time? reach me at: http://deano.me/contact/

  • Great Working !!!

    Thanks Dean

  • No problem, great to help!

  • Dear Dean,

    I don't know why but I can't get it working for siteŁ http://www.wilek666.vot.pl/index.php?route=information/o-firmie.
    I'm using oc 1.5.6.1

    I add to seo_url.php:
    if ($url[0] == 'o-firmie') {
    $this->request->get['route'] = 'information/o-firmie';
    }

    and to table oc_url_alias
    INSERT INTO oc_url_alias (query, keyword) VALUES ('o-firmie=o-firmie', 'o-firmie');

    Could you help me ?

  • Thank you ! Works just fine !
    Congrats

  • Yes i can take a look for you, ill try contact you but if you dont hear from me please fill in my contact form on this site 🙂

  • how can i change url "https://mysite.com/index.php?route=account/return/insert" to https://mysite.com/return. please help

  • Thank U for such a helpful post. it really helped me. i want to re-write search page. can you please help?

  • Opencart 2.x it is not working for me.

  • If you want contact-us instead of contact then
    seo_url.php
    if ($url[0] == 'contact-us') {
    $this->request->get['route'] = 'information/contact';
    }
    url_alia
    query contact-us=contact-us=contact-us=contact-us
    keyword contact-us=contact-us

  • Opencart 2.x has many changes to the database, I have been doing allot of optimizations for OpenCart 2.x but unfortunately have not been able to post about this yet but as soon as I have the time I will write an article.

    In the meantime if anyone needs any OpenCart experts please contact us on our website: https://webdesires.co.uk

  • for premium support please visit my company site: https://webdesires.co.uk

  • for premium support please visit my company site: https://webdesires.co.uk


Leave a Reply: