CodeIgniter Forums
Weird routing behaviour - 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: Weird routing behaviour (/showthread.php?tid=38555)



Weird routing behaviour - El Forum - 02-11-2011

[eluser]mmackay[/eluser]
It's been one of those weeks and this has been bugging me for a couple of days now. I could just be having a bad week and not spotting the obvious but my routing isn't working as intended. It worked perfectly on v.1.7.3 but when I upgraded to 2.0 it kinda went wrong. Here's the low down...

This is my .htaccess file:

Code:
RewriteRule ^([a-z0-9]+)/audio/([a-z0-9\-]+)(/?)$ index.php/$1/tracks/$2 [L,NC]
RewriteRule ^([a-z0-9]+)/video/([a-z0-9\-]+)(/?)$ index.php/$1/mixes/$2 [L,NC]
RewriteRule ^([a-z0-9]+)(/?)$ index.php/user [L,NC]

The idea being that I'm capturing the following URLs:

http://www.domain.com/username/audio/audio-item-token <-- Displays the users audio
http://www.domain.com/username/video/video-item-token <-- Displays the users videos
http://www.domain.com/username <-- Displays all the users items

My CI routing file is as follows:

Code:
$route['(:any)/tracks/(:any)'] = 'user/tracks';
$route['(:any)/mixes/(:any)'] = 'user/mixes';
$route['(:any)'] = 'user/index';

The main $route['(:any)'] = 'user/index'; actually sits at the bottom of the routes file.

I'm then using the uri_segment (temporarily) to retrieve the username, validate etc. etc. However, this just simply doesn't work. I'm getting a CI 404 returned for any URL. Strangely though, it works for the users admin URL:

Code:
RewriteRule ^([a-z0-9]+)/admin(/?)$ index.php/$1/admin [L,NC]
RewriteRule ^([a-z0-9]+)/admin/(audio|video)(/?)$ index.php/$1/admin/$2 [L,NC]

Code:
$route['(:any)/admin'] = 'admin';
$route['(:any)/admin/audio'] = 'admin/audio';
$route['(:any)/admin/video'] = 'admin/video';

The admin rules appear below the other routes.

I don't get how the admin rules work but the main site ones don't?

A quick view of the log (when calling http://www.domain.com/mikemackay for example) shows as:

DEBUG - 2011-02-11 11:40:43 --&gt; Config Class Initialized
DEBUG - 2011-02-11 11:40:43 --&gt; Hooks Class Initialized
DEBUG - 2011-02-11 11:40:43 --&gt; Utf8 Class Initialized
DEBUG - 2011-02-11 11:40:43 --&gt; UTF-8 Support Enabled
DEBUG - 2011-02-11 11:40:43 --&gt; URI Class Initialized
DEBUG - 2011-02-11 11:40:43 --&gt; Router Class Initialized
ERROR - 2011-02-11 11:40:43 --&gt; 404 Page Not Found --&gt; mikemackay

I'm stumped. Anyone got any ideas? Can you spot something simple that I'm overlooking?

- Mike


Weird routing behaviour - El Forum - 02-11-2011

[eluser]InsiteFX[/eluser]
The (:any) should be at the end of your routes.
Code:
$route['(admin/(:any)'] = 'admin';
$route['(admin/audio/(:any)'] = 'admin/audio';
$route['(admin/video/(:any)'] = 'admin/video';

InsiteFX


Weird routing behaviour - El Forum - 02-11-2011

[eluser]mmackay[/eluser]
[quote author="InsiteFX" date="1297446735"]The (:any) should be at the end of your routes.
Code:
$route['(admin/(:any)'] = 'admin';
$route['(admin/audio/(:any)'] = 'admin/audio';
$route['(admin/video/(:any)'] = 'admin/video';

InsiteFX[/quote]

That doesn't make any difference, if I set the main user up with:

Code:
RewriteRule ^([a-z0-9]+)/audio/([a-z0-9\-]+)(/?)$ index.php/user/audio/$2 [L,NC]
Code:
$route['user/audio/(:any)'] = 'user/audio';

It still gives me a 404 if I call the following:

http://domain.com/mikemackay/audio/some-audio-file-token


Weird routing behaviour - El Forum - 02-11-2011

[eluser]mmackay[/eluser]
Everything now seems to be working, I've updated my .htaccess and routes code as follows:

Code:
RewriteRule ^([a-z0-9\-]{1,16})(/?)$ index.php/$1/user [L,NC]
RewriteRule ^([a-z0-9\-]{1,16})/audio/([a-z0-9\-]+)(/?)$ index.php/$1/user/audio/$2 [L,NC]
RewriteRule ^([a-z0-9\-]{1,16})/video/([a-z0-9\-]+)(/?)$ index.php/$1/user/video/$2 [L,NC]

Code:
$route['(:any)/user'] = 'user/index';
$route['(:any)/user/audio/(:any)'] = 'user/audio';
$route['(:any)/user/video/(:any)'] = 'user/video';

I'm pretty sure it's an oversight on my part along the way somewhere. Things started to work around the time I placed the character length limit on the .htaccess regex - although I don't know if this was the exact fix.

Thanks for reading and thanks to InsiteFX for the reply.

- Mike