Use this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
It should be noted that this will also effect all HTTP requests, including POST, which will subsequently effect all requests of this kind to fall under this redirection and may potentially cause such requests to stop working.
To resolve this, you can add an exception into the first RewriteRule
to ignore POST requests so the rule is not allowed to them.
# To externally redirect /dir/foo.php to /dir/foo excluding POST requests
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R=302,L,NE]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…