I've been working for hours in this and can't find information about it. Well yes, there is information, but I can only find separate solutions that don't work put toguether.
I'm trying to start from this:
RewriteCond %{REQUEST_URI} \.(php)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
The code works, I get the trailing slash. BUT if I put a non existing filename it returns a 301. For example:
http://www.example.com/thisfiledoesnotexist.php
The first header response is a 301, and it's logical, because it tries to return:
http://www.example.com/thisfiledoesnotexist.php/
And then it returns the 404, but this way it takes two requests to return a 404.
I supose that many people found this problem before, I believe it's a common issue, but I haven't found the solution to it.