# Mille — Apache config for Hostinger.
# Copied to dist/ root by Astro's static-file pipeline (anything in /public
# ships verbatim). Apache reads this on every request to the docroot.

# ---------------------------------------------------------------------------
# Compression — Brotli first, gzip as fallback.
# ---------------------------------------------------------------------------
<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS \
    text/html \
    text/css \
    text/plain \
    text/xml \
    text/javascript \
    application/javascript \
    application/json \
    application/xml \
    application/rss+xml \
    application/atom+xml \
    application/xhtml+xml \
    application/x-font-ttf \
    application/x-font-truetype \
    application/x-font-opentype \
    image/svg+xml \
    image/x-icon \
    font/ttf \
    font/otf
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE \
    text/html \
    text/css \
    text/plain \
    text/xml \
    text/javascript \
    application/javascript \
    application/json \
    application/xml \
    application/rss+xml \
    application/atom+xml \
    application/xhtml+xml \
    application/x-font-ttf \
    image/svg+xml
</IfModule>

# ---------------------------------------------------------------------------
# Cache-Control — content-hashed assets get 1 year + immutable; the rest
# get short caches with revalidation.
# ---------------------------------------------------------------------------
<IfModule mod_headers.c>

  # /_astro/* — Astro emits content-hashed filenames (e.g.
  # /_astro/petite-l.CaAkE.webp). Filename changes when content changes, so
  # we can cache forever and tell the browser to never revalidate.
  <FilesMatch "\.(webp|avif|jpg|jpeg|png|gif|svg|woff|woff2|ttf|otf|mp4|webm|mov)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # /js/alpine-3.14.1.min.js and other pinned-version files in /js/ — same
  # logic: the file name carries the version so it's safe to cache forever.
  <FilesMatch "^alpine-[0-9.]+\.min\.js$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # HTML — short cache so content edits roll out within a day, but the
  # browser still uses the cached copy on a back-button navigation.
  <FilesMatch "\.html$">
    Header set Cache-Control "public, max-age=3600, must-revalidate"
  </FilesMatch>

  # XML manifests (sitemap, RSS) — 1 hour.
  <FilesMatch "\.xml$">
    Header set Cache-Control "public, max-age=3600"
  </FilesMatch>

  # .htaccess and other dotfiles must never be served. Apache usually blocks
  # them by default but make it explicit.
  <FilesMatch "^\.">
    Require all denied
  </FilesMatch>

  # Security baseline. These don't help with performance but make the site
  # safer at zero cost.
  Header set X-Content-Type-Options "nosniff"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set Permissions-Policy "interest-cohort=(), browsing-topics=()"
</IfModule>

# ---------------------------------------------------------------------------
# Pretty URLs — strip .html from request paths. Astro emits dir-style
# routes (/sofa/milano/index.html) so this is mostly defensive; lets older
# bookmarks still resolve.
# ---------------------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine On

  # If a request comes in for /foo.html, redirect to /foo/.
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.+)\.html$ /$1/ [R=301,L]

  # If /foo/ doesn't resolve to a directory but /foo/index.html exists, serve it.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
  RewriteRule ^(.+?)/?$ /$1/index.html [L]
</IfModule>

# ---------------------------------------------------------------------------
# MIME types — Apache may not know modern formats by default. Force these
# so the browser doesn't fall back to text/plain or refuse to display them.
# ---------------------------------------------------------------------------
<IfModule mod_mime.c>
  AddType image/webp .webp
  AddType image/avif .avif
  AddType font/woff2 .woff2
  AddType font/woff .woff
  AddType video/mp4 .mp4
  AddType video/webm .webm
</IfModule>

# ---------------------------------------------------------------------------
# Error pages — point to Astro-rendered 404 if it exists.
# ---------------------------------------------------------------------------
ErrorDocument 404 /404.html
