Custom 404 Not Found Page

Written by in Tutorials on September 24, 2013  |  6 Comments

custom-404-not-found-page

OpenCart use the same template for 404 Not Found and the “empty” notification, it’s not_found.tpl. For OpenCart 1.5.6, this “not found” template is called from 10 controllers. The real not found with 404 return header is only from error/not_found controller. Other controller use it to show “no products in category”, “empty shopping cart”, etc.

To create our custom 404 template we just need to tell error/not_found controller to use other template, instead of modificating other 10 controller.

Open catalog/view/theme/default/template/error/ folder then copy not_found.tpl and rename the copy file to 404_not_found.tpl. Open 404_not_found.tpl and change something there, to make sure we notice the different between 404 page and the default not found template.

Now we tell error/not_found controller to use our custom template. Open catalog/controller/error/not_found.php, put code below before $this->children:

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/404_not_found.tpl')) {
   $this->template = $this->config->get('config_template') . '/template/error/404_not_found.tpl';
} elseif (file_exists(DIR_TEMPLATE . 'default/template/error/404_not_found.tpl')) {
   $this->template = 'default/template/error/404_not_found.tpl';
}

And screenshoot below is example of what I get when accessing index.php?route=product/categori

opencart-custom-404-not-found-page

To make it easy for you to test this tutorial, you can download the file at the end of article. Extract and upload catalog/ and vqmod/ folder to your site.

Download1027 downloads
Tags: ,

About the Author

The man behind OpencartNews and EchoThemes.

View all posts by

6 Comments on "Custom 404 Not Found Page"

  1. Shah December 14, 2015 at 4:39 am · Reply

    Hi Qahar,

    I tried all above tweaks but it didnt work and i noticed its just because i use custom template. Could you suggest how do i create custom 404 error page in my custom template. Do i have to replace template not_found.php file with yours 404_not_found.php file and not_found.tpl file with your 404_not_found.xml file. It will work?

    I am not tech guru, so please assist. Thanks!

  2. Nick January 14, 2015 at 9:53 am · Reply

    Great idea and thanks for sharing the code. Would this work on OpenCart v2 in the same way?

    Thanks, Nick

  3. Ken December 16, 2014 at 12:51 am · Reply

    i got this error when i try to insert googleanalytic code on my open-cart installation

    ” forbidden you don’t have permission to access /admin/index.php>
    additionally a 404 not found error was encountered when trying to use an error document to handle it.”

    Please i need your help

  4. werepair August 12, 2014 at 6:09 am · Reply

    Hi, you say to add the following code

    catalog/controller/error/not_found.php, put code below before $this->children:
    if (file_exists(DIR_TEMPLATE . $this->config->get(‘config_template’) . ‘/template/error/404_not_found.tpl’)) {
    $this->template = $this->config->get(‘config_template’) . ‘/template/error/404_not_found.tpl’;
    } elseif (file_exists(DIR_TEMPLATE . ‘default/template/error/404_not_found.tpl’)) {
    $this->template = ‘default/template/error/404_not_found.tpl’;
    }

    do we replace this code that is already in the file?

    if (file_exists(DIR_TEMPLATE . $this->config->get(‘config_template’) . ‘/template/error/error_404.tpl’)) {
    $this->template = $this->config->get(‘config_template’) . ‘/template/error/error_404.tpl’;
    } else {
    $this->template = ‘default/template/error/error_404.tpl’;
    }

  5. swed May 25, 2014 at 1:19 pm · Reply

    Well done Qahar, Is this a version independent approach?

    • Qahar May 28, 2014 at 1:27 am ·

      Yes I think it’s independent. As far as I remember – on OpenCart 1.5.x – other controller is not load not_found controller directly. Instead only use the not_found.tpl to show empty data.

Leave a Comment

comm comm comm