Colbydude's avatar

Trailing Slashes causing 404s on Lumen apps?

Just curious to see if anyone else was having this issue.

From a fresh Lumen installation adding a test route as such:

$app->get('/test', function() use ($app) {
    return $app->welcome();
});

Trying to access from /test will work fine, but /test/ will return a 404. Should this not be working? The .htaccess has the rule to handle trailing slashes, as well as the NGINX rules packed in with Homestead and Forge.

0 likes
14 replies
arabsight's avatar

Same problem, the links generated by the paginator won't work.

lumen.app/api/v1/articles/?page=2     (throws NotFoundHttpException)

please share if you find anything.

1 like
iRazaSyed's avatar

I thought i was doing something wrong. So this issue is common!

1 like
bestmomo's avatar

For pagination, till it works just genrate links like this :

$links = str_replace('/?', '?', $collection->render());

Or paginate on client side as I did in this example.

1 like
arabsight's avatar

I added this to nginx config to remove the trailing slashes:

rewrite ^(.*[^/])$ $1/ permanent;
1 like
bashy's avatar

That's to do with how you setup and read the URIs...the router on Lumen or Laravel does not see the forward slashes.

Colbydude's avatar

What exactly do you mean by setup @bashy? Are there ways to hardcode in the forward slashes? That could be potentially useful for the small app I'm trying to convert to Lumen from just stock PHP.

Regardless, Lumen and Laravel both ship with the .htaccess (or Homestead or Forge for NGINX configs) to handle URIs with trailing slashes to 301 redirect to the URI without the trailing slash. It's just clearly not working with Lumen. ¯_(ツ)_/¯

bashy's avatar

@Colbydude The trailing slash is web server side, if your app doesn't work with them it's because your web server setup isn't reading the trailing slash and rewriting it to the index.php file.

Colbydude's avatar

Right, I get that. I'm just having trouble figuring out why trailing slashes will work with a fresh Laravel 4/5 app, but not Lumen when sharing the same server setup. So I'm trying to figure out what makes Lumen different from Laravel in that regard. Especially if a handful of other people are having the same issue. I just don't know the deep inner-workings of either framework well enough to know what to look for, nor have the time to do the research. =\

bashy's avatar

Maybe the router that's used is totally different to the Laravel one...

1 like
cnenu's avatar

I added this to nginx config to remove the trailing slashes:

rewrite ^(.*[^/])$ $1/ permanent;

That rule matches paths without a trailing slash and adds one, but Lumen (at least as of v5.0.8) ignored routes with trailing slashes.

I used the rule below to remove the trailing slash and there was much rejoicing.

rewrite ^/(.*)/$ /$1 permanent;
brendanabbott's avatar

I too am facing this with a Lumen + Homestead setup. @dhersam where in the nginx file did you place that rule?

bashy's avatar

@brendanabbott In the server block? Probably within the location section if you have it setup to do try_files.

Please or to participate in this conversation.