CodeIgniter Forums
Trailing Forward Slash Added to some routes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Trailing Forward Slash Added to some routes (/showthread.php?tid=88874)



Trailing Forward Slash Added to some routes - ltarrant - 11-25-2023

I am experiencing an issue where CI4 framework seems to be adding a trailing forward slash to some routes. However, my htaccess file is setup to redirect and remove trailing slashes. Its then getting in a redirect loop between the framework and the htaccess file.

Only the following routes seem to be affected:

/product
/accessory
/collection
/page

Generally these routes take a second URI part (slug) to pass to the controller. My original route entry was just as follows:

    $routes->get('product/(:any)', 'ProductPages::view/$1');
    $routes->get('accessory/(:any)', 'ProductPages::accessory/$1');
    $routes->get('collection/(:any)', 'Collections::view/$1');
    $routes->get('page/(:any)', 'Pages::view/$1');

Somewhere along the way I tripped over the fact if the second part was missing we got into the redirect loop rather than just returning a 404 response. 

So I added the following explicit routes without any additional URI parts.

    $routes->get('product', 'ProductPages::view');
    $routes->get('accessory', 'ProductPages::accessory');
    $routes->get('collection', 'Collections::view');
    $routes->get('page', 'Pages::view');

For some reason the issue persists with a forward slashing getting added to the end.

However, if I create a new route to the same controller method:

    $routes->get('product_test_route', 'ProductPages::view');

It works fine and doesn't add a forward slash. I thought perhaps the additional rules like:

 $routes->get('product/(:any)', 'ProductPages::view/$1');

Might be making the framework do something to add the forward slash but even if I temporally comment those out the issue persists.

Another thought was that possibly the issue is corrected but the redirect for these URL are now cached in the browser. However, I tried clearing browser cache and even switching browser and the problem persists.
I now a bit stumped.


RE: Trailing Forward Slash Added to some routes - kenjis - 11-25-2023

Cannot reproduce it with `spark serve`. So It seems something wrong with your apache configuration.

// Routes.php
$routes->get('product/(:any)', 'Home::index/$1');

Run local server:
$ php spark serve

Navigate to http://localhost:8080/product

I see:
404
Can't find a route for 'get: product'.


RE: Trailing Forward Slash Added to some routes - ltarrant - 11-29-2023

I figured this out in the end.

Those routes happen to have actual directories in the public folder which store images used.

It turns out the default behaviour of Apache is to automatically add the trailing slash where an actual directory exists.

It took a while to realise this was the issue as I had dismissed it being an Apache issue as I had stripped everything away in the htaccess file except the Ci4 index.php rewrite.

Turns out I just needed to add the following to the htaccess to stop the behaviour.

DirectorySlash Off

Oh well at least my code got a bit of a tidy up in the wasted time trying to find the issue.