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. http://bit.ly) so I can access content by using this kind of format http://blood4lifeid.org/uac1

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.

P.S:
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. http://yoursite.com/content-title-that-is-seo-friendly)

Advertisements

12 comments

  1. aaaa · February 15, 2012

    thanks, nice tut

  2. Cristian Sanninostian · February 16, 2012

    Hi, can you explain how to apply this scenario http://yoursite.com/content-title-that-is-seo-friendly ? i’ve one or more controller and i need to have all urls dinamic, somethink like this test.com/?variable=Value&variable2=value2 in test.com/value/value2.
    Thanks a lot 🙂

    • Eru · March 2, 2012

      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

  3. chiru · January 7, 2014

    Helllo there????
    I want to send parameters with url can you tell me how????

  4. Dhanang · October 20, 2014

    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

    • Eru · November 4, 2014

      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)
      }
      
  5. rafaelfigueiredo91 · March 2, 2015

    This tutorial simple save hour of work, thanks
    Work fine on CI 3.0

  6. freddysidaurukFreddy · June 6, 2015

    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

    $route[‘(:any)’]=”artikel/slug/$1″;
    $route[‘login’]=”login”;
    $route[‘dashboard’]=”dashboard”;
    $route[‘dashboard’]=”dashboard”;

    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

    • Eru · June 7, 2015

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

      $route[‘login’]=”login”;
      $route[‘dashboard’]=”dashboard”;
      $route[‘(:any)’]=”artikel/slug/$1″;

  7. gourabvarma · August 29

    Nice tutorial

  8. gourab varma · August 29

    Nice tutorial dude great work

  9. Devinder Saini · October 20

    Hi My route is config as:

    $route[‘listingdetail/(:any)’] = “sabonclick/listingdetail/$1”;

    It gave me output as: http://sabonclick.com/listingdetail/13107

    But my requirement is like : http://sabonclick.com/listingdetail/NameOfVendor

    Kindly help me.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s