# IONOS / Apache shared-hosting config for the static `out/` build.
#
# Drop the contents of `out/` (after `npm run build:static`) into your
# IONOS webspace document root. This file is shipped from `public/`
# so it ends up at `out/.htaccess` automatically.
#
# Mirrors the policy in `public/_headers` (Cloudflare Pages / Netlify
# format) — keep them in sync.

# ── Directory index ─────────────────────────────────────────────────
# Be explicit so a webspace whose Apache default doesn't list
# index.html as a DirectoryIndex doesn't 403 the bare domain root.
DirectoryIndex index.html

# ── Pretty-URL rewrite ──────────────────────────────────────────────
# Next static export emits a flat tree where /en/faq is served by
# /en/faq/index.html (or /en/faq.html in some configurations). Without
# a rewrite Apache 404s on the bare path. Try the file first, then a
# directory, then `<path>.html`.
<IfModule mod_rewrite.c>
  RewriteEngine On

  # ── Force HTTPS ──────────────────────────────────────────────────
  # Redirect every plain-HTTP request to its HTTPS equivalent. We
  # check both %{HTTPS} (set when Apache terminates TLS itself) and
  # the X-Forwarded-Proto header (set by IONOS / Cloudflare / any
  # reverse proxy that terminates TLS upstream and forwards plain
  # HTTP to the origin). Without the X-Forwarded-Proto check we'd
  # infinite-loop on those setups, since %{HTTPS} would always be
  # off even after the redirect lands as HTTPS at the edge.
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # If the request is for an actual file or directory, serve it.
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Otherwise, append `.html` and try again — handles /en/faq → /en/faq.html.
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^(.+?)/?$ $1.html [L]
</IfModule>

# ── Security headers ────────────────────────────────────────────────
<IfModule mod_headers.c>
  Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval' https://static.cloudflareinsights.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; font-src 'self' data:; connect-src 'self' https://cloudflareinsights.com; worker-src 'self' blob:; frame-src 'self'; frame-ancestors 'self'; form-action 'self'; base-uri 'self'; object-src 'none'"
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(self)"
  # HSTS — 2 years + subdomains + eligible for the preload list. Only
  # safe to set on a domain that is HTTPS-only and intends to stay so.
  Header set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
  # COOP — isolates the top-level window from cross-origin popups.
  # `same-origin` is the strongest setting; nothing on the site relies
  # on a cross-origin popup talking back to it.
  Header set Cross-Origin-Opener-Policy "same-origin"
  Header set Cross-Origin-Resource-Policy "same-site"
</IfModule>

# ── Caching ─────────────────────────────────────────────────────────
# Long-lived immutable cache for hashed/static asset directories.
#
# `<FilesMatch>` matches against the basename only — not the URL path —
# so leading-slash patterns silently never match. The path-based long
# cache below uses `SetEnvIf Request_URI` to flag requests under the
# stable directories, then `Header set ... env=` applies the immutable
# header to those flagged requests. The mod_expires defaults set the
# baseline; we don't bother trying to override ExpiresDefault per path
# because the immutable Cache-Control already wins.
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 hour"
  ExpiresByType text/html "access plus 5 minutes"
</IfModule>

<IfModule mod_setenvif.c>
  # Tesseract OCR core wasm + per-language traineddata: stable.
  SetEnvIf Request_URI "^/tesseract/" SUBHUNT_LONG_CACHE
  # Optimized landing-page webp variants: content-stable filenames.
  SetEnvIf Request_URI "^/images/landing/.*\.webp$" SUBHUNT_LONG_CACHE
  # Next.js hashed JS/CSS chunks.
  SetEnvIf Request_URI "^/_next/static/" SUBHUNT_LONG_CACHE
</IfModule>

<IfModule mod_headers.c>
  Header set Cache-Control "public, max-age=31536000, immutable" env=SUBHUNT_LONG_CACHE
</IfModule>

# ── MIME types ──────────────────────────────────────────────────────
# IONOS Apache usually has these, but be explicit so a fresh
# webspace doesn't surprise us.
<IfModule mod_mime.c>
  AddType image/webp .webp
  AddType application/wasm .wasm
  AddType application/json .traineddata
</IfModule>

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