CodeIgniter 3 404 Error

Dealing With CodeIgniter 404 Error Tutorial

Views: 614

Codeigniter is a loosely based MVC (Model View Controller) framework which allows you to create manageable PHP applications easily and in a lesser time, but before you use any MVC it is important for you to know how that MVC works and its structure. Else you may run into many issues and make your application a headache for you. As the 404 issue on codeigniter may occur because you might have missed something very little.

The error will look like:

404 Page Not Found

The page you requested was not found.

with the default controller for 404 errors

Let’s talk about the few reasons that may cause your site not to load and display 404 page instead.


Check Your htaccess FileDealing With CodeIgniter  404 Error Tutorial

Check your .htaccess file
.htaccess are the configuration files used in apache based hosting to enable disable functionalities of apache, .htaccess are very handy to use and you can do a lot of amazing things by including one .htaccess file in your domain’s root directory.But before you include .htaccess feature in your website you should must know what exactly it will do. Else the website may not load anymore or show error pages. Here is a sample .htaccess I am using in my codeigniter website. This .htaccess removes the index.php from URL. I hope you may find this helpful.

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]


Check your routes configured properly
Check your routes configured properly
Codeigniter comes with an amazing feature of routing which allows you to let create custom routes for different controllers, it automatically senses the URL and loads the configured controller for that route. It might happen sometimes you configure a wrong route and codeigniter fails to find the controller, this may cause a 404 error to appear on your project. Make sure you have configured proper routes. Recheck and verify them if you are not sure.You can find the routes.php inside the yoursite_root_directory/application/config directory.Example:I am adding few examples here to show how it works, make sure you configure the routes properly follow the tips below.

Set your default controller

The default controller:

The default controller is what sets controller for your home page. Every time you go to an URL of codeigniter site and home page is displayed, a default controller is loaded to display that page. If this route is not configured you will see default codeigniter home page instead of the page you want to set as home page. And if you have configured it incorrect then surely you will see a 404 error page each time you visit the URL. Assume I have a controller named home which contains the home page function i.e function index (){} and I want it to load each time I visit my URL then I have to set the default controller like:$route[‘default_controller’] = ‘home/index’;

Other controllers:

You can really do amazing things with codeigniter controllers, you can generate beautiful SEO friendly routes with codeigniter but you can also mess up everything with incorrect routing.

Lets assume I have a about_us() function under the home controller. And we want a url like so we will set the route as given below:


This route configuration will route the about_us function of home controller to it is pity simple yet effective for beautiful and SEO friendly URL structures.

Routing With Arguments:

Take an example that you have a controller with function which takes 3 arguments let’s assume city, state and zip to for processing user requests and the function name is “services” and lies inside the service controller, for this scenario, so the function will look like:function services($city=’’, $state=’’, $zip=’’)


//Your coding


And let’s assume your URL looks like :

The repeated service in URL you want to remove and want it to look like

For this you will need to configure a route to load the controller service and call function services then pass arguments city, state and zip when “ “ URL is used, so you will have to set a route as given below:


And the last thing I am going to include in this post is exclusively for linux based hosting which made me dig much more inside and do a long research to resolve as it was certainly an very unusual problem that I saw.

404 Error on Linux Based Servers
404 Error On Linux Based Servers
If you have done everything correct and configured everything properly yet getting 404 error and your hosting is with linux based server then the issue may be with naming strategy you have used for controllers and models. Linux based servers are case sensitive which means that “about-us” and “About-us” both are different files, and codeigniter suggests uppercase for the first alphabet of any controller and model name. So please make sure that all files start with uppercase inside model and controller directory and this will make the 404 error go. This worked in my case and I am confident about this will help you too.

Comments: 0

Your email address will not be published. Required fields are marked with *