Promote your contents FREE, contents such as WebSites, YouTube channels, YouTube videos, Facebook, Instagram, LinkedIn, Twitter, Pinterest and many more.
Learn how to do routing in CodeIgniter! Discover step-by-step methods to create SEO-friendly custom URLs using routes.php file.
Opening content automatically in secounds...
Or
This content was added on Spotenjin at 12 Feb 2022 and got 1214 visits untill now.
In modern web application development, clean, user-friendly, and SEO-optimized URLs play a pivotal role in delivering a great user experience and ranking higher on search engines. By default, the CodeIgniter PHP framework maps incoming browser requests directly using a strict Controller/Method/Parameter URL hierarchy (such as `example.com/product/detail/12`). However, CodeIgniter includes a robust routing engine that allows developers to re-map these default URLs into customized, descriptive paths like `example.com/products/laptops`. Masterful handling of CodeIgniter routing allows you to completely decouple your URL structures from your internal file hierarchy.\n\nTo begin configuring custom routes, you must first locate the designated routing file within your CodeIgniter application directory structure. If you are working with legacy CodeIgniter 3, routes are defined inside the `application/config/routes.php` file using a global array called `$route`. For developers utilizing modern CodeIgniter 4, custom routes are declared in the `app/Config/Routes.php` configuration file using the `$routes` service object. Both versions operate under similar logical principles: matching an incoming URL string pattern and directing it to the correct controller class and method.\n\nThe fundamental syntax for creating a basic, static custom route is straightforward. Suppose you have a controller named `Pages` with a method called `about_us()`, which normally resolves to `example.com/pages/about_us`. To simplify this into a clean URL like `example.com/about`, you define a static route rule. In CodeIgniter 3, you write `$route['about'] = 'pages/about_us';`. In CodeIgniter 4, you use the HTTP verb method `$routes->get('about', 'Pages::about_us');`. Whenever a site visitor accesses `/about`, CodeIgniter routes the request internally to the designated controller method without altering the address bar.\n\nBeyond static pages, real-world web applications heavily depend on dynamic URLs that pass parameters, such as user IDs, product slugs, or blog dates. CodeIgniter provides convenient wildcards and placeholders to match dynamic URI segments effortlessly. The two most common wildcards are `(:num)`, which matches numeric characters only, and `(:any)`, which matches any sequence of characters. For instance, to map a blog post URL like `example.com/blog/my-first-post` to a `Blog` controller's `view($slug)` method, you write `$route['blog/(:any)'] = 'blog/view/$1';` in CodeIgniter 3 or `$routes->get('blog/(:any)', 'Blog::view/$1');` in CodeIgniter 4. The `$1` acts as a back-reference variable containing the value captured by the wildcard placeholder.\n\nFor complex web applications requiring strict validation on parameters, regular expression (regex) routing offers complete control over matching criteria. Instead of standard wildcards, you can insert regex patterns directly into the route key. For example, setting `$route['products/([a-z]+)/([0-9]+)'] = 'products/filter/$1/$2';` ensures that the route executes only if the first parameter consists of lowercase alphabetic characters and the second parameter is strictly numerical. This technique prevents invalid requests from reaching your controllers and adds an extra layer of security.\n\nCodeIgniter 4 introduces advanced routing features that make application architecture even cleaner, such as route grouping and HTTP verb-based routing. Route grouping allows you to wrap administrative or modular routes together under a shared prefix. By defining `$routes->group('admin', function($routes) { ... });`, all nested routes automatically inherit the `/admin/` path prefix. Furthermore, verb-based routes—including `$routes->get()`, `$routes->post()`, `$routes->put()`, and `$routes->delete()`—allow developers to quickly build RESTful API endpoints where identical URIs perform different operations based on the incoming HTTP method.\n\nA common issue developers face when creating custom URLs is the presence of `index.php` in the browser address bar. To achieve truly clean, professional custom URLs, you must remove `index.php` by enabling URL rewriting on your Apache or Nginx web server. On Apache servers, creating a simple `.htaccess` file in your root CodeIgniter directory with `RewriteEngine On` rules routes all incoming web requests through the index file invisibly. Once configured alongside your custom routes file, your application yields short, elegant, and highly performant URLs.\n\nIn conclusion, understanding how to configure custom routing in CodeIgniter is an essential skill for modern PHP developers. Whether you are mapping simple static pages, dynamic blog slugs, or structured administrative portals, custom routes provide the flexibility needed to build scalable, SEO-friendly web applications.