CodeIgniter Forums
URL Routing: With or without trailing slash - 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: URL Routing: With or without trailing slash (/showthread.php?tid=10032)



URL Routing: With or without trailing slash - El Forum - 07-16-2008

[eluser]jonnyjon[/eluser]
Hi,
I have a route set up:

$route['directory/(:any)'] = "controller/function/$1";

The problem is that...
This works: http://example.com/directory/whatever
But this doesn't work: http://example.com/directory

I know I could add a second route:
$route['directory'] = "controller/function";

But is there a way to incorporate a reg ex to handle both cases in one statement?

This this (but this doesn't work):
$route['directory[\/*](:any)'] = "controller/function/$1";


URL Routing: With or without trailing slash - El Forum - 07-16-2008

[eluser]richthegeek[/eluser]
Code:
directory(\/.*|)



URL Routing: With or without trailing slash - El Forum - 07-16-2008

[eluser]jonnyjon[/eluser]
Thanks!

$route['directory(\/*|)([A-Za-z0-9]*)'] = "controller/function/$2";

Works great!


URL Routing: With or without trailing slash - El Forum - 07-17-2008

[eluser]Colin Williams[/eluser]
Code:
$route[’directory(.*)’] = “controller/function$1”;



URL Routing: With or without trailing slash - El Forum - 07-17-2008

[eluser]Bramme[/eluser]
Colin's is the shortest, I use it too... Works like a breeze.


URL Routing: With or without trailing slash - El Forum - 07-17-2008

[eluser]jonnyjon[/eluser]
Yes, Colin's method is good but would match any number of sub-directories. ex: http://example.com/directory/subdir/subdir/subdir/

I just want to match:
http://example.com/directory/subdir/ OR http://example.com/directory

This is my updated reg expression:
$route['directory\/?([a-zA-Z0-9_]*)'] = "controller/function/$1";


URL Routing: With or without trailing slash - El Forum - 07-17-2008

[eluser]Colin Williams[/eluser]
True, jonnyjon. The method I lined out is what I do to effectively 100% alias a Controller, which I think is extremely useful. When naming a Controller, it's good to name it in the context of the role it serves in your application. However, the URL should be named with consideration to a number of factors, like a users mental model, applications brand, etc. The above method allows for that with 0 headache... not that you needed or asked for that advice, but there it is Smile