# Clean public URLs: /index.php becomes / and /contact.php becomes /contact.
DirectoryIndex index.php
AddDefaultCharset UTF-8

RewriteEngine On

# Redirect direct browser requests for the homepage to the site root.
RewriteCond %{THE_REQUEST} \s/+(.*/)?index\.php(?:[?\s]) [NC]
RewriteRule ^index\.php$ /%1 [R=301,L,NE]

# Redirect direct requests for other PHP pages to extensionless URLs.
RewriteCond %{THE_REQUEST} \s/+(.*/)?([^/\s?]+)\.php(?:[?\s]) [NC]
RewriteRule ^(.+)\.php$ /%1%2 [R=301,L,NE]

# Internally serve an existing PHP file when its clean URL is requested.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
