I’ve seen a lot of people requesting help on figuring out routing issues on CodeIgniter (2.1.0 as of this entry).

A few of them have problems creating a route to a controller folder structure (/application/controllers/folder/controller), but really it is the same as creating a custom route to a non ‘folder’ed’ controller, you just have to define the variable in the routing path.

Here is our scenario, I have a URL of http://example.com/account/manage/4123245/jakub

So if we analyze our URI, we have:
account – folder
manage – controller (with only an index class)
4123245 – a numeric value (input)
jakub – a string slug value (just extra input)

From that we need to make sure that codeigniter understands our URI (and it will not by default!).
so we edit our /application/config/routes.php

$route['account/manage/(:num)/(:any)'] 	= "account/manage/index/$1/$2";

Index is the key here, as we need to route to the class (which is the index), the values $1, $2 are just input (1st, 2nd respectively) to show it goes to the index class.

There you have it, no more route issues with folders in your controller structure.

Happy Code Igniting!



  • kelvin

    having trouble with this

  • Info

    does this require setting up an htaccess file?

  • Jakub

    Might want to post the actual ‘trouble’ you are having, so that I could possibly suggest a solution.

  • Jakub

    This has nothing to do with .htaccess appart from setting up your initial .htaccess file for clean urls, this is routing, as in from one place to another.

  • Peter Nemeth

    i’ve got problem when i want use routing and pagination, too

    my routing:

    $route['forum/(:num)/page/(:num)'] = “forum/id/$1/page/$2″;

    it works until i want to go back from page 2 to page 1…in codeigniter if we go back to the first page in url it shows no number…in my opinion:

    page 2 -> …./forum/2/page/2 it works

    page 1 -> …./forum/2/page/ and here is the problem…it shows 404 Page Not Found

    WHY? (sry for my bad english :) )

  • Jakub

    Peter, why not simply add an additional static route for your ‘page 1′ issue:

    $route['forum/(:num)/page/'] = “forum/id/$1/page/1″;

    You basically account for the page 1, OR you would modify your link / url scheme in your pagination setup.

  • Peter Nemeth

    i was trying to put there route to page1

    $route['forum/(:num)/page/'] = “forum/id/$1/page/1″;

    but it does not solved the problem.. :( maybe i just delete from url “page”, but after that the url will be ugly

  • Hawk

    what if in the slug we have a string containing spaces e.g slug = this is new 1.
    when i used this slug without space it works fine
    but when with spaces then it says page not found. plz help, i am new to CI

  • Jakub

    You cannot use spaces in a URI, this is basic HTML URI 1on1. If you force it, it will not work. You may use CI’s url_title() to convert strings with spaces into strings that are SAFE for the URI