Custom 404 Not Found Page
Written by Qahar in Tutorials on September 24, 2013 | 6 Comments
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
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.
6 Comments on "Custom 404 Not Found Page"
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!
Great idea and thanks for sharing the code. Would this work on OpenCart v2 in the same way?
Thanks, Nick
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
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’;
}
Well done Qahar, Is this a version independent approach?
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.