RewriteEngine On

# Force non-www to www and HTTP to HTTPS in a single rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?memomaxpro\.org [NC]
RewriteRule ^(.*)$ https://www.memomaxpro.org%{REQUEST_URI} [L,R=301,NE]

# Redirect /index.html to /
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]

# Redirect .html URLs to clean URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^([^.]+)\.html$ /$1 [R=301,L]

# Internally rewrite clean URLs to .html files
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.]+)$ $1.html [L]

ErrorDocument 404 /404.html

# Redirect any /index, /index.html, /index.php to homepage
RewriteCond %{THE_REQUEST} /index(\.html?|\.php)? [NC]
RewriteRule ^index(\.html?|\.php)?$ https://www.memomaxpro.org/ [R=301,L]