RewriteEngine On
RewriteBase /

# Force HTTPS and non-WWW canonical host
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.kampung62\.co$ [NC]
RewriteRule ^ https://kampung62.co%{REQUEST_URI} [R=301,L]

# Redirect index.html to homepage
RewriteCond %{THE_REQUEST} \s/+index\.html(?:[\s?]|$) [NC]
RewriteRule ^index\.html$ https://kampung62.co/ [R=301,L]

# Redirect explicit .html URLs to clean URLs
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html(?:[\s?]|$) [NC]
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

# Internally serve clean URLs from matching .html files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

# Prefer a dedicated 404 page if available
ErrorDocument 400 /index.html
ErrorDocument 401 /index.html
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
ErrorDocument 500 /index.html
ErrorDocument 503 /index.html
