Slim and FallbackRessource

Im using Slim for the first time, it all works well and i am very happy with it.
But I have a technical question about the server configuration and the rewrite rules we are all using.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

These rules are rewriting all request to index.php
The thing is to do that, i prefer to use the FallbackResource.
It does the same and it doesn’t invoke the mod_rewrite engine.

FallbackResource /index.php

It all works well, all my routes are triggered, except the main /
The browser is curiously trapped in a kind of 404 response where the connection never ends until the browser tab is closed. I’ve made some fast checking and the weird happens in App.php in the respond() method where the $response->getHeaders() are written in the foreach() loop.

I’m working behind an Nginx proxy and $response->getProtocolVersion() returns “1.1”.
When i change $response->getProtocolVersion() by “1.0”, the loop ends well and then the code keep executing until it fails for some reason.

I couldn’t go further as the deadline of the project im working on is coming soon and Slim is working great with the original RewriteRules, but still, im wondering what is happening. I believe i would prefer to make the use of FallbackResource if possible to avoid the overhead of a mod_rewrite call.

Can you give this a try:

DirectoryIndex /index.php
FallbackResource /index.php

1 Like

It works :slight_smile:
Thanks so much SVL :sunny:
As the Apache documentation insist in saying mod_rewrite shouldn’t be used if they are other ways to achieve the same, these 2 lines should replace the rewrite rules in the Slim’s documentation and skeleton.
btw, thanks again !

My own two cents…

Server configuration and optimization is up to the end user and isn’t something Slim should be trying to document. We should provide minimal examples of configuration to get most users up and running as quickly as possible. If they wish to further optimize, that is beyond the scope of Slim.

The mod_rewrite method will likely work for more people than the FallBackResource method. Also, often rewrite rules are needed such as rewriting all http requests to https, removing or adding a www. prefix, etc. So my own feeling is that changing the docs and skeleton would mean that it works out of the box in fewer instances.

I do like how Silex covered this issue. Their skeleton doesn’t provide a .htaccess file.