Url Suffix in Codeigniter

Codeigniter is a great Framework, it’s been around for quite some time now. Some might say this makes it a bit long in the tooth. Personally I don’t agree, it’s still actively developed, faster than most (much faster than Laravel) and allows you to still write raw PHP when you want to.

One of the niggles though is the way it handles the use of Url suffix generation. You can set any suffix you like in the site config file however then ALL url’s have the suffix applied. While this might be ok for the frontend, it’s not always ideal in the backend and not exactly flexible. This is especially annoying if, like me, you declare your css and JavaScript files within site_url() – as it then affixes the suffix to the end rendering the link useless.

Another, safer way of adding a suffix is to use URI Routing. You can specify different Routing for different URLs and send them to different controllers. You can also add URL suffix on only the pages you want. An example of this is here:

$route['default_controller'] = 'page';
$route['404_override'] = 'My404';
$route['(:any)\.html'] = 'page/pages/$1';
$route['admin/dashboard/(:any)'] = 'admin/dashboard/$1';

As you can see we have a default “frontend” controller (page), a Route for any pages and a Route for pages that have the beginning path admin/dashboard.

Any page that doesn’t start admin/dashboard has the suffix .html applied.

Now what no-one tells you about setting up suffix generation this way is that it breaks the standard 404 page generation. As a result you need to then setup a new class to either create your own 404 page or point to the default 404 view file.

It’s pretty easy to do this though, but you do need to remember to add the class you create to the 404_override setting in the Route file as shown above. the class can be as simple as:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class My404 extends MY_Controller {

public function __construct()

public function index()

In this example I’ve simply pointed to the existing 404 errors view page that comes with Codeigniter but you can use any view file. The important thing to remember is to make sure you set the header status as “404”.