Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /

# ── Force HTTPS ───────────────────────────────────────────────────────────────
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ── Security headers ──────────────────────────────────────────────────────────
<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set X-XSS-Protection "1; mode=block"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(self)"
  Header always unset X-Powered-By
  Header always unset Server
</IfModule>

# ── Compression ───────────────────────────────────────────────────────────────
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript
  AddOutputFilterByType DEFLATE application/javascript application/json
  AddOutputFilterByType DEFLATE image/svg+xml application/xml
</IfModule>

# ── Browser caching ───────────────────────────────────────────────────────────
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/html                "access plus 0 seconds"
  ExpiresByType application/javascript   "access plus 1 year"
  ExpiresByType text/css                 "access plus 1 year"
  ExpiresByType font/woff2               "access plus 1 year"
  ExpiresByType font/woff                "access plus 1 year"
  ExpiresByType application/font-woff2   "access plus 1 year"
  ExpiresByType image/png                "access plus 1 day"
  ExpiresByType image/jpeg               "access plus 1 day"
  ExpiresByType image/gif                "access plus 1 day"
  ExpiresByType image/webp               "access plus 1 day"
  ExpiresByType image/avif               "access plus 1 day"
  ExpiresByType image/svg+xml            "access plus 1 day"
  ExpiresByType image/x-icon             "access plus 1 year"
  ExpiresByType application/json         "access plus 0 seconds"
  ExpiresByType application/xml          "access plus 1 hour"
</IfModule>

<IfModule mod_headers.c>
  # Next.js hashed bundles are content-addressed — cache forever
  <FilesMatch "\.(js|css|woff|woff2|ttf|eot)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "\.(png|jpg|jpeg|gif|webp|avif|svg|ico)$">
    Header set Cache-Control "public, max-age=86400, stale-while-revalidate=604800"
  </FilesMatch>
</IfModule>

# ── Custom 404 — never show LiteSpeed / Apache default error page ─────────────
ErrorDocument 400 /404.html
ErrorDocument 401 /404.html
ErrorDocument 403 /404.html
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html

# ── Clean URL routing (Next.js static export) ─────────────────────────────────
# File layout after `next build` with output:"export":
#   /             → index.html
#   /about        → about.html
#   /index/slug   → index/index/slug.html   ← Next.js nests "index" segment one level deeper

# 1. Pass through real files (_next/static, images, sitemap.xml, rss.xml …)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# 2. Pass through real directories only when URL already ends with /
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^ - [L]

# 3. /index/<slug> → /index/index/<slug>.html
#    Next.js places routes under /index/[slug] inside a nested index/ subdirectory
RewriteRule ^index/([^/]+)/?$ /index/index/$1.html [L]

# 4. All other routes: serve <path>.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]

# 5. Everything else → Glemad 404 page (never show LiteSpeed default)
RewriteRule ^ /404.html [L]
