Options -Indexes
RewriteEngine On
RewriteBase /

# -------------------------------------------------------
# 0. Block direct directory access — return 403
# -------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ - [F,L]

# -------------------------------------------------------
# 1. Strip .html — redirect /page.html → /page (301)
# -------------------------------------------------------
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# -------------------------------------------------------
# 2. Internally serve /page as /page.html (if file exists)
# -------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ $1.html [L]

# -------------------------------------------------------
# 3. Catch-all — any unknown URL falls back to index.html
# -------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]

# -------------------------------------------------------
# 4. ErrorDocument fallback (for servers without mod_rewrite)
# -------------------------------------------------------
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
