This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

How to Create Custom Codeigniter 404 Not Found Page

Updated on  17th August

4 Min Read
codeigniter 404 page not found
Reading Time: 4 minutes

404 Not Found is perhaps the most common type of error users encounter on the internet. It is one of the most common errors users experience during web browsing. Since the error could potentially impact the user experience, CMS and frameworks have come up with creative ways of handling the error.

Codeigniter offers a simple way of creating custom 404 error pages in which the website or the application could tackle the situation creatively and redirect the visitor to the appropriate page(s). This way, the developers have complete control over the user journey and could make sure that the visitor doesn’t bounce off the website. Codeigniter 404 Page Not Found error offers a great opportunity to retain user engagement and present related links and/or information about services or products to the visitors.

Why 404 Not Found Error Occurs

404 Not Found is the HTML error code that indicates a particular situation in which the connection between the server and the client is working correctly, but the server could not find the resource requested by the client.

The underlying causes of the error are several, but the general process is similar in all cases. The server has received a request for a resource that is non-existent on the server of PHP hosting. A very common cause of this situation is the broken links that point to pages or resources that have been removed (or taken down) by the website administrator.

In this article, I will show you how you can create a fully customized 404 error page in Codeigniter. I will start by creating the controller and then move on to set up the routes.

You might also like: Use Elasticsearch in Codeigniter

Create the Controller for Custom Codeigniter 404 Page Not Found Error

I will start by creating a class for the controller in the application/controllers/ folder. I have named this class My404 which has an index() method for loading the view.

You might also like: Pass Data From Controller to View in CodeIgniter

Set up the Route

Next, I will set up the route in the application/config/routes.php file. This file will contain the following line that overrides the default route:

Creating the View

I will now describe the view of the custom 404 page in the application/views folder.  you will notice that in the following complete code of the view that I have redirected the visitor to the homepage.

You might also like: Create Multiple Database Connections in CodeIgniter

Alternative Method of Creating the Controller

Another (and much simpler) way of creating a custom 404 page for your Codeigniter project is to add the following code to the system/application/controllers/error.php file. In this code, I have extended the controller class into a custom class named Error. This class contains the method error_404() that outputs the “404 – Not Found” error if it encounters the 404 code in the header.

Now create MY_Router.php  in the system/application/libraries/ folder and add the following code to it:

Finally, I will call the 404 Page manually by calling the show_404() method that checks the two parameters passed in its array and shows error if it discovers that a parameter is missing. Use the following statement below to manually call the 404 Page.

Conclusion

In this short tutorial, I have demonstrated two methods that you can use for creating (and calling) a custom designed 404 error page for your Codeigniter application. A custom 404 error page is an ideal way of retaining visitors on the website and redirecting them to appropriate pages on the website. This greatly reduces the bounce rate of the websites and ensures that the visitors remain interested in your website. If you have further questions and queries about creating custom 404 pages for Codeigniter projects, do post them in the comments below.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.