CodeIgniter Forums

Full Version: Route regular expression should not match according to regexpal.com, but it does anyway
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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.

El Forum

[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]

El Forum

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