CodeIgniter Forums
Clean URLS and the Routes config file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Clean URLS and the Routes config file (/showthread.php?tid=35634)



Clean URLS and the Routes config file - El Forum - 11-05-2010

[eluser]Steve Wanless[/eluser]
I have an application where I use the routes config;

Code:
$route[':any'] = "page/view/$1";

to achieve clean (and short) URLs to get something like this

example.com/about-us

instead of

example.com/page/view/about-us

Seeing this is the last line in my routes file, I have to manually enter a re routing line for every controller.
I would prefer to not have to enter something for each controller, otherwise they all get re routed to the pages controller.

Has anyone had this same problem? How did you deal with it?

Is it possible to extend the CI Routes file to go to a specific controller/method instead of the 404 page??

Thanks,


Clean URLS and the Routes config file - El Forum - 11-09-2010

[eluser]amosmos[/eluser]
this seems to interest a lot of users, and i would be very glad to hear some experts opinion on how to achieve this...

i know how to route everything to a specific controller with the route[':any'] method, but would like to know what's the best way to do it when you only want some urls to go there.

what i mean is that i want something that does this:
1. if there is a controller that fits the name in the 'controller' segment of the url, go to it.
2. if there isn't, go to another, specified, controller. in this controller i can put the check for the slug and either show the right page, or show a 404.

thanks,
amos


Clean URLS and the Routes config file - El Forum - 11-09-2010

[eluser]amosmos[/eluser]
Found it! Smile

CI2 now support $route['404_override'] in order to specify a controller\method that will be called when no existing controller\method is found.

Make sure to specify both the controller and the method, as the 'index' method will not be called automatically.

thanks,
amos


Clean URLS and the Routes config file - El Forum - 11-09-2010

[eluser]Steve Wanless[/eluser]
Superb!! Fast, simple solution without having to hack the CI Core.

Thanks.