RewriteEngine on

# Rewrite rule to handle directories with trailing slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?)/$ /$1/index.php [L]

# Redirect requests for .php files to remove the extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Rewrite rule to internally add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# Handling 404 errors
ErrorDocument 404 /404
