php - Redirecting all URLs with extension to no extension, including in subdirectories -


i'm looking way in redirect https://example.com/foo.php https://example.com/foo , https://example.com/foo/ https://example.com/foo.

there copious solutions posted on stack overflow, none of them transpire address instance user tries access file such https://example.com/foo/bar/baz.php.

could kindly propose solve .htaccess?

the code have is:

## hide .php extension # externally redirect /dir/foo.php /dir/foo rewritecond %{the_request} ^[a-z]{3,}\s([^.]+).php rewriterule ^ %1 [r=301,l]  rewritecond %{the_request} ^[a-z]{3,}\s([^.]+)/\s rewriterule ^ %1 [r=301,l]  ## internally redirect /dir/foo /dir/foo.php rewritecond %{request_filename}.php -f rewriterule ^(.*)$ $1.php [l] 

try these rules:

rewriteengine on   ## hide .php extension # externally redirect /dir/foo.php /dir/foo rewritecond %{the_request} ^[a-z]{3,}\s(.+?)\.php[\s?] [nc] rewriterule ^ %1 [r=302,l,ne]  ## unless directory, remove trailing slash rewritecond %{request_filename} !-d rewriterule ^(.+)/$ /$1 [ne,r=302,l]  ## internally redirect /dir/foo /dir/foo.php rewritecond %{request_filename}.php -f rewriterule ^(.+?)/?$ $1.php [l] 

remember clear browser cache before testing.


Comments