Routing is the core element in any MVC framework. So in this tutorial, we are going to learn about CodeIgniter routing. It has user and search engine friendly URL or URI routing system.

In simple words, we can create the user define URL and redirect a user to any specific URL. There is only one to one relationship between the controller class and the URL path.

Codeigniter routing has their own systematic way to develop clean URL path. Following is the default structure of URL within CodeIgniter.

Right after the domain name, there is a controller name which is represented as the first segment in URL.

After the first segment which is the controller name, there is method name considered as the second segment.

In the last, there is the third segment right after the second segment which is considered as the parameter of the controller method.

In custom application development sometimes we need custom URL or routing system for application and in this case, CodeIgniter helps us to modify the default routing system.

Customize CodeIgniter Routing

We can easily define custom URL and their format to access them from Browser within “routes.php” file which is available under the config folder of the application.

Default Routes

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

In the above code, you will observe and an associative array having name “$route”. This array will decide how the user will access the application and what will be the routing for individual controller and method.

Points to remember:

  • You must have to define a default controller otherwise the application will be redirected to 404 page.
  • You can replace the name of default controller with your own controller name.
  • The default controller will be considered as the main controller of the application.
  • You can define custom 404 error page in the definition of second index “$route[‘404_override’]”. If it is empty then it will use the default CodeIgniter 404 error page.
  • The third index “$route[‘translate_uri_dashes’]” is used to declare that you want to use “-“ within your URL or not. By default its value is false but if you will make its value true then you can use “-“ within your URL but “-“ is not supported as a naming convention of controller and method instead of this you can use underscore “_”.

Sample Routes

$route['CodeIgniter/Introduction'] = 'codeigniter/index';
$route['CodeIgniter/Setup'] = 'codeigniter/setup';
$route['CodeIgniter/demo'] = 'codeigniter/code';

In the above code example within “$route” array we are declaring exact path and on the right-hand side we are declaring the exact method of the controller which will entertain that request. So now when I will call the following URL in the browser.

The response of above URL will be entertained by the following controller and method.

   class codeigniter extends CI_Controller{

   	public function index() { 



Route Wildcards

Even that we can customize are routing with wildcard entries call by using regular expressions within the routing array. We have two kinds of wild cards which we can use within the route which are as follow.

  1. (:num) – It will allow numbers in the segments of URL.
  2. (:any) – It will allow all characters in the segments of URL.

Wildcards (:num)

$route['CodeIgniter/(:num)'] = 'codeigniter/index/$1';

Wildcards (:any)

$route['CodeIgniter/(:any)'] = 'codeigniter/index';