Do I undertand correctly that this is what you want:
When have a request like:
http://example.com/foo.php
you want it to
1) Return a 301 redirect to the browser with the URL
http://example.com/foo
, then
2) When the requests for any path whose name can resolve to a file having a .php extension, serve that file.
Assuming that the host name is consistent, I think it can be simpler, something like this:
# Redirect any request with a .php extension to have no extension
RewriteRule ^(.*)\.php $1 [R=301,L]
# Extensionless requests that are .php files should be served directly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Note: you may need slightly different syntax if your doing this from a server context (e.g. in a Virtual Host file) versus if you're doing this from an .htaccess file in the root of your server -- in particular, I think in the latter case you may need to add a forward slash. See the note in the "Per Directory Rewrites" section of [
httpd.apache.org...]
It looks like you're doing work in the regexes that Apache already does for you with query string parameters and # anchors, as well as paths names.
So for example, I think this rewrite would correctly serve a request to
http://example.com/path1/path2/foo?param1=bar1¶m2=bar2
would get served if, for example, there were a file on your web server with a (unix) path like
/var/www/example.com/path1/path2/foo.php
I agree with Jim -- these things are best worked out with a blazingly simple example.
And one other thing if you have access to them: RewriteLog and RewriteLogLevel can be tremendously helpful. Unfortunately, they are only settable in the server or virtual host contexts, not .htaccess files. But in one case, I was able to coerce a web host to add a rewrite log that I could read to our virtual host config while I worked out a gnarly problem.
Good luck!
Tom