如何重写URL以包含变量并在允许目录时删除php

I have the following htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?user=$1

which works as expected for example

http://example.com/index.php?user=USERNAME -> http://example.com/USERNAME

However I have created a form on the page index.php which posts to /directory/save.php

How do I remove .php while allowing for the directory so that I can post to /directory/save/ instead?

if it is going to one and only such file in /directory then probably hard code it by adding following before the above rules?

RewriteRule ^directory/save$ /directory/save.php [L]

directory/ might have it's own rewrite rules and not have a physical save.php, that's why !-f might not work. Try adding a new rewrite condition to stop rewriting for directory/

RewriteCond %{REQUEST_URI} !^directory

Try to check this one if it's rewriting /directory/save.php file to directory /directory/save/

RewriteCond %{REQUEST_URI} ^/directory/save\.php$
RewriteRule ^(.+) /directory/save/ [NC]