RewriteEngine On

# Redirect /page.php to /page (ignore ajax and includes folders)
RewriteCond %{REQUEST_URI} !ajax/ [NC]
RewriteCond %{REQUEST_URI} !includes/ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Internal rewrite: /page to /page.php if the file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# General routing to index.php for CMS/dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
