CodeIgniter Routes Trick – Removing Controller Names from the URI to Keep URLs Short.

CodeIgniter is great because you can separate your content from your styling with controllers, models, and views. Another great thing is when you create a website with CI, you automatically use friendly URLs (if you use an .htaccess file). If you have a bunch of unrelated pages however, you might wonder how you can keep those URLs as tiny as possible without having thirty different controllers for each page. For example, I have a CI application with a home controller that has about 20 different functions inside of it. To access each one of these functions, I would normally visit http://localhost/home/contact or http://localhost/home/about_us. Since we don’t really need home in the URL, I have found a way to remove it from the URL, still allowing other controllers to function normally. Here’s how to do it:

Open up your routes.php file inside the application/config folder in your CI app and add the following line to the end of the routes.php file:

$route['^(?!ezstore|ezsell|login).*'] = "home/$0";

This line uses a regular expression that means, If a visitor goes to any url EXCEPT ezstore, ezsell, or login, redirect them to the home controller, and the function inside the home controller ($0). This way, every time we call a function from the home controller, such as contact, about_us, services, etc., we can snip home out of the URL and keep our URLs short and pretty.

The end!



22 Responses to “CodeIgniter Routes Trick – Removing Controller Names from the URI to Keep URLs Short.”

  1. It’s really a great tip! But it could be better if, instead of manually write the exceptions (name of the controllers), it was possible to read the controllers directory and automatically fill the names…

    Do you think it’s possible? Any idea how?

  2. You would have to write a loop before the routes come into play to identify all of the different functions inside the classes. Then, you would have to make sure none of them are named the same or choose a default route if functions are named the same within multiple controllers. Seems like a lot of work! There may be a better way…

    You may be able to just loop through all of your classes and try searching for a function that exists. That would be the easiest route I think. Then, you would just have to specify the names of the controller classes.

  3. Trourbito says:

    Уроки счастья или как встретить свою любовь на Западе
    Расскажет и научит вас вебсайт http://www.mydatelove.com

  4. Ryan says:

    Thanks for this!just what I needed!

  5. nick yeoman says:

    Wow, I was really stuck on my project until I found this, exactly what I needed.

  6. sirviejo says:

    Great tip!

    i will play with routes and then post my experience.

  7. Paresh says:

    The only challenge I see is the speed of the page. Does frequent use of routing slows the page loading? I am facing similar challenge in my application and avoiding routing for the fear that it may slow the page loading.

  8. Vot says:

    Take a look at my routes.php. I posted it at CodeIgniter forums so the code looks fine there. http://codeigniter.com/forums/viewthread/48510/#744516

    I guess that’s the best solution so far.

  9. Oden says:

    you helped me so much! ty

  10. Billy Shall says:

    Previously I was rewriting a majority of my controllers, this will keep it much more simple. Great tip!

    Thanks

  11. Stiofan says:

    I was looking for a solution like this for hours, thank you so much!

  12. Zoran says:

    You can also store routes regular expressions in your database and then override _set_routing function from the Router class in your own MY_Router class and after passing the $this->routes = $route;
    You can add the ones from the database to the $this->routes property and that’s it, CI will know about the routes that could be created dynamically.

  13. A little bit of internet surfing and observed your weblog appears to be a lttle bit screwed up in my K-meleon browser. But fortunately rarely anyone makes use of it any more however you might want to take a look into it.

  14. Thanks For This Information. But I Need Not Show Any Controller And Function Name In My URL. I Only Want To Send Parameters Only To Different Functions. I want to Setting My Root File Which Parameter Goes To Which Function. Is It Possible Or Not.

    WAQAS ALI – LAHORE – PAKISTAN.

  15. Exterminator business is great. Always something new to see :)

  16. Damon Poole says:

    Thanks, this is just what I was looking for! :D

  17. zian says:

    tnx guys!

  18. James says:

    Thanks, it saves my time. :)

  19. A great deal, this valuable put up is truly the nicest about this successful subject. My wife and i harmonise employing your a conclusion and can thirstily look ahead to your favorite arriving fresh news. Revealing i am grateful won’t only get an adequate amount of, to your extraordinary quality from your simply writing. I’m able to quickly receive your ultimate feed to vacation told of upgrades. Vital employment and a lot becoming successful on the home business ventures! Please justification my very own very poor Words currently never your really language.

  20. Dustin says:

    Worked like a charm. Thanks

  21. You actually make it seem really easy along with your presentation but I find this matter to be really something that I think I might never understand. It seems too complex and very wide for me. I’m looking forward in your next submit, I’ll attempt to get the cling of it!

Leave a Reply