CodeIgniter Forums
Route regular expression should not match according to regexpal.com, but it does anyway - 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: Route regular expression should not match according to regexpal.com, but it does anyway (/showthread.php?tid=15692)



Route regular expression should not match according to regexpal.com, but it does anyway - El Forum - 02-11-2009

[eluser]hal10001[/eluser]
Code:
^([a-zA-Z-]+/)*[a-zA-Z-]+/?$

That should match the following:

category-name/sub-category-name/sub-sub-category-name/page-name

However, it should NOT match:

category-name/sub-category-name//sub-sub-category-name/page-name

Notice the double slash. The route is as follows:

Code:
$route['^([a-zA-Z-]+/)*[a-zA-Z-]+/?$'] = "homepage";

Right now I'm just redirecting to the homepage for testing purposes. When it doesn't match, it should throw a 404. So on a URL with a double slash it should be throwing a 404.


Route regular expression should not match according to regexpal.com, but it does anyway - El Forum - 02-12-2009

[eluser]pistolPete[/eluser]
Your regular expression is correct.
The reason why CodeIgniter accepts the double slashes is because apache (which you are probably using) "converts" double slashes to single slashes.
Assuming that you've set
Code:
$config['uri_protocol']    = "AUTO";
or
Code:
$config['uri_protocol']    = "QUERY_STRING";
CodeIgniter uses the QUERY_STRING environment variable.

Have a look at
Code:
function _fetch_uri_string()
in ./system/libraries/URI.php

But even if you set
Code:
$config['uri_protocol']    = "REQUEST_URI";
which is unaltered by apache, the multipe slashes are "removed" by
Code:
function _explode_segments()
in ./system/libraries/URI.php

Conclusion:
If you want to filter out bad requests containing double slashes, you better add the following lines to your .htaccess file:
Code:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]



Route regular expression should not match according to regexpal.com, but it does anyway - El Forum - 02-23-2009

[eluser]hal10001[/eluser]
Thanks pistolPete... very helpful!