CodeIgniter, PHP, Troubleshooting, Web

CodeIgniter router config for dynamic url (url shortener or seo friendly url)

Here’s the scenario, I want my CodeIgniter to be able to works similar to url shortener services (e.g. so I can access content by using this kind of format

There’s so many tricks to this. We can use mod_rewrite on some specific pattern, or force our CI to use one Master-Controller, or rewrite CI router mechanism.

But I am going to use the easiest one, only changing CI router.php config
At first I use this on CI routes.php

$route['(:any)'] = "incidents/detail_shortened/$1";

As expected, It’s kind of working, but it makes all of my controller inaccessible, that is because any ‘/controllername/parameter/’ format will match with ‘(:any)’ and will be redirected to our ‘incidents/detail_shortened/’.

To stop controllers redirected by the CI router, I have to explicitly define all of my controllers on the routes.php first (since it’s handled in sequence)

This is the code for that:

$route['default_controller'] = "welcome";
$route['404_override'] = 'help/show404';

// define all 'normal' possible routing path
$route['callbacks'] = 'callbacks';
$route['callbacks/(:any)'] = 'callbacks/$1';

$route['faqs'] = 'faqs';
$route['faqs/(:any)'] = 'faqs/$1';

$route['help'] = 'help';
$route['help/(:any)'] = 'help/$1';

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

$route['welcome/(:any)'] = 'welcome/$1';

// the last resort (dynamic)
$route['(:any)'] = "incidents/detail_shortened/$1";

And now it works 🙂 all controller will he handled normally, and my shortened dynamic url will be properly
handled by my `incidents` controller.

This applies to CodeIgniter 2.0 (i am not sure whether it works for CI < 2.0)

You can also apply this scenario for your SEO friendly dynamic URL (e.g.


12 thoughts on “CodeIgniter router config for dynamic url (url shortener or seo friendly url)

    1. It’s about the same with the scenario above, you have to map all of your controllers first and on the last line you put the code below
      to make sure your content controller handle for your SEO friendly url.

      /// the last resort (dynamic)
      $route['(:any)'] = "content_controller_here/view/$1";

      I am not sure whether CI will parse the variables separately, but you may have to manually parse the ‘/value/value2/’ parameters

  1. I get an error on 404_override. The custom error 404 not show. Because there are $route[‘(:any)’] = ‘home/page/$1’.

    If I remove that route. The custom error show up normally. I use HMVC to my CI. What’s my mistake? My CI version is 2.2.0. Please help me,. This is my 404 route $route[‘404_override’] = “not_found”; not_found is my controller.

    Thank you

    1. The things – using $route[‘(:any)’] will force all routing to go to your `home/page/` controller. So your 404 will not work at all.
      have you tried to define 404_override before your custom routing?

      One solution is to set a prefix. For example : $route[‘content/(:any)’]
      Or if it’s not your Use Case, you need to define a logic in your landing controller.

      if (contentNotFound) {
          // call your 404 controller (or use HMVC to run your 
          // 404 action)
  2. hello dear, let sya i have router like this $route[‘(:any)’]=”artikel/slug/$1″;
    why another controller not working, let your tutorial said should define one by one controller so everything will be working well here is little script


    my question is how can i make it simple not write router to much cause i just change one $route[‘(:any)’]=”artikel/slug/$1″;, Thanks for your respond

    1. Try to reverse it. From your specific routing and put the catch-all routing on the bottom


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s