Htaccess rewrite if .php url extension remove it

I also want 301 to redirect any url that ends with .php. Then I internally redirect everything that ends with /before the name /plus .php. They both work great when they are alone. But putting them in the same .htaccess, I get a The page isn't redirecting properly.

What am I doing wrong here?

RewriteRule ^(.*)\.php$ /$1/ [R=301,L]

RewriteRule ^(.*)/$ $1.php [QSA,L]
+4
source share
1 answer

These rules should work for you:

RewriteEngine On

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L,NE]

# To internally forward /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
+5
source

All Articles