Understanding CodeIgniter's URI routing system

When developing a web application using CodeIgniter, it is essential to have a good understanding of its URI (Uniform Resource Identifier) routing system. URI routing plays a crucial role in determining how requests are handled and how specific controllers and methods are executed.

CodeIgniter's URI routing system allows you to achieve clean and user-friendly URLs, improving the overall user experience and making your application more search engine friendly.

Anatomy of a URL in CodeIgniter

Before diving into the URI routing system, it's important to understand the anatomy of a URL in CodeIgniter. A typical URL in CodeIgniter has the following format:

  • http://example.com/ represents the domain name and the base URL of your application.
  • index.php is the main entry point to your application.
  • controller refers to the name of the controller class that will handle the request.
  • method represents a specific method within the controller that will process the request.
  • params are optional parameters that can be passed to the method.

Default Routing Behavior

By default, CodeIgniter uses a "segment-based" approach for routing. This means that the segments of the URL path are used to determine the controller, method, and parameters.

For example, consider the following URL: http://example.com/index.php/blog/view/123. In this case:

  • blog represents the controller class name.
  • view represents the method name within the blog controller.
  • 123 is a parameter that will be passed to the view method.

CodeIgniter maps the segments of the URL to the corresponding controller, method, and parameters automatically, following a specific naming convention.

Customizing URI Routing

CodeIgniter allows you to customize the URI routing behavior according to your specific needs. This is useful when you want to create more meaningful and SEO-friendly URLs or hide certain parts of the URL.

To define custom URI routes, you need to modify the routes.php file located in the application/config directory. In this file, you can specify your custom routes using a simple syntax.

Here's an example of a custom route definition:

$route['products/(:num)'] = 'catalog/product/$1';

In this example, whenever a URL matches the pattern products/(:num), it will be routed to the catalog controller's product method, passing the numeric value as a parameter.

You can define as many custom routes as you need in the routes.php file, and they are processed in the order they are defined. The first matching route will be applied.

Advanced Routing Techniques

CodeIgniter's URI routing system also supports more advanced techniques, such as regular expression matching, route placeholders, and wildcards.

Regular expression matching allows you to define more complex route patterns. For example, you can match URLs with specific patterns like news/2021/06.

Route placeholders dynamically match segments of the URL and pass them as parameters to the controller methods. For instance, you can define a route like users/(:any)/photos to handle URLs like users/johndoe/photos.

Wildcards enable you to define catch-all routes that match any URI segment. This technique is powerful when you want to handle global filters or create a flexible routing structure.


Understanding CodeIgniter's URI routing system is vital for developing robust and user-friendly web applications. By customizing URI routes, you can create clean and SEO-friendly URLs, improving the overall user experience.

Remember to make good use of the routes.php file in the application/config directory to define your custom routes. Additionally, consider exploring more advanced routing techniques like regular expression matching, route placeholders, and wildcards to handle complex routing scenarios.

noob to master © copyleft