Why is my rewrite rule resulting in "400: Bad Request"?

I have these rewrite rules in my site configuration:

RewriteEngine on
RewriteRule ^app.php - [L]
RewriteRule ^(.*)$ app.php$0 [L]

The goal is that all requests should be appended to the front controller "app.php". The last line of the rules is causing my server to respond with "400: Bad Request" no matter what path I try, even an empty path. I have enabled the rewrite log and attempted two paths: "/" and "/login". This is what the log says:

(2) init rewrite engine with requested uri /
(3) applying pattern '^app.php' to uri '/'
(3) applying pattern '^(.*)$' to uri '/'
(2) rewrite '/' -> 'app.php/'
(2) local path result: app.php/
(2) init rewrite engine with requested uri /login
(3) applying pattern '^app.php' to uri '/login'
(3) applying pattern '^(.*)$' to uri '/login'
(2) rewrite '/login' -> 'app.php/login'
(2) local path result: app.php/login

According to the logs it should succeed. If I remove the last line of the rewrite rules and visit the URL "app.php/login" then it works as expected.

Does anybody have any idea why this isn't working?


Note: There are no errors in the error logs.
Note: The resulting paths ("app.php/", "app.php/login") work perfectly when the last rewrite rule is removed.


Solution 1:

I figured it out. For some reason I had to append a "/" to the rewrite path:

RewriteRule ^(.*)$ /app.php$0 [L]

Otherwise, apparently, any path would rewrite to:

example.comapp.php/

I have another server with the same Apache version where this did not occur so I'm still confused, but at least it works.