# Clean URLs: no .php, trailing slash. Works in any folder or document root.
# CSS/JS/images use $base from PHP (SCRIPT_NAME).
RewriteEngine On

# Add trailing slash for directory-like URLs (no file extension, not already ending with /)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^/]+$
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} ^(.+[^/])$
RewriteRule .* %1/ [R=301,L]

# Redirect .php requests to clean URL (trailing slash), preserve query string
RewriteCond %{THE_REQUEST} \s(/[^ ]*)\.php[\s?]
RewriteRule .* %1/ [R=301,L,QSA]

# Serve index for base URL
RewriteRule ^$ index.php [L]

# Rewrite /path/ to path.php (only if not an existing file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1.php [L]
