URL Routing

Next Page Previous Page

What are Routes?

URLs in Codeigniter are a short path to redirect users from one page to another. We predefined routes to match and respond to URL requests, if any of the predefined route requests don’t match then Codeigniter throws a 404 exception that says ‘page not found’.

Example: http://www.mywebexample.com/url_routing


  • The controller -is mapped to the controller name that responds to the URL requests.
  • Method – is mapped to the method in the controller.
  • Parameter – parameters are optional.

Let’s create URLs for the Application

To create a route in our application we need to add routes in  application/config/routes.php

Examples :

$route['default_controller'] = 'welcome';

$route[‘profile’] = 'name';

$route['phone’'] = 'phone/create';

$route['edit/:id'] = 'number’/edit';

$route['update/:id'] = 'number/update';

$route['delete/:id'] = 'number/delete';

$route['404_override'] = '';

$route['translate_uri_dashes'] = FALSE;


Default_controller: Every time we run the application the default route will take us to the welcome page 

404_override: when the URL doesn’t match any of the predefined routes the 404_override route will display an exception page 

 Setting Custom Routing rules:

The routing rules can be found in the route.php file inside the application/config. Folder, under $routes array we can add our custom rules and criteria for routing that can be classified in 2 types, wildcard or regular expression.

  • Wildcard:

Types in wildcard:

  • num-series
  • any-series

num-series: Only contains a series of numbers.


$route['(contact/:num)'] = edit/profile/$1;  


It means when we'll pass URL http://www.contact.com/num/1

 $1: we can pass any number here.

 any-series: Only contains a series of characters.



$route['(contact/:num)'] = edit/profile/;  


It means when we'll pass URL http://www.contact.com/num/qwer

Any series of characters can be passed in the last segment.

  • Regular Expression:

REGEX, Regular expressions are special strings that describe search patterns


$route['contact'(a-zA-Z0-9]+)'] = edit/profile;  

URL suffix:

We can add a suffix to our URLs in the config.php file located inside the application/config folder 



$config['url_suffix'] = '.abc;  

URL before adding a suffix to it:



URL after adding a suffix to it:



Next Page Previous Page

Treding Tutorial

Popular Tutorials

to Learn now