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)

Advertisement

2 Responses to “CodeIgniter router config for dynamic url (url shortener or seo friendly url)”


  1. 1 aaaa February 15, 2012 at 2:32 pm

    thanks, nice tut

  2. 2 Cristian Sanninostian February 16, 2012 at 8:55 am

    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 :)


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 )

Connecting to %s




About The Blog

Trying to share many settings or programming problems that I find during my work-hours :)

The Writer

Erwin Maulana Saputra a Software Engineer who live in Indonesia, in love with this field since junior high-school while creating ASCII or 320x200, 256bit games :) and now somehow making money from web stuff *wink* Used to work at DotSeven and Mitrais and currently freelancing until January 2011
LinkedIn

 

September 2011
M T W T F S S
« Aug   Dec »
 1234
567891011
12131415161718
19202122232425
2627282930  

Follow

Get every new post delivered to your Inbox.

Join 715 other followers