# ==================================================================
# gildask.com — SPA React / Vite sur hébergement Apache (o2switch)
# À placer dans le dossier public_html, à côté du index.html du build.
# ==================================================================

# ------------------------------------------------------------------
# 1. Réécriture des routes
#    Sans ce bloc, /maintenance renvoie une 404 : Apache cherche un
#    dossier de ce nom alors que c'est React Router qui gère la route.
# ------------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # Forcer HTTPS
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Forcer une seule version du domaine — décommenter UNE des deux.
  # Sans www :
  # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  # RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
  #
  # Avec www :
  # RewriteCond %{HTTP_HOST} !^www\. [NC]
  # RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Ne jamais réécrire un fichier ou un dossier qui existe vraiment
  # (assets, images, sitemap.xml, robots.txt…)
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Tout le reste part vers index.html : React Router prend la main
  RewriteRule ^ index.html [L]
</IfModule>

# ------------------------------------------------------------------
# 2. Cache
#    Le piège classique de Vite : les fichiers de /assets/ portent un
#    hash dans leur nom, on peut donc les mettre en cache un an.
#    index.html, lui, ne doit JAMAIS être mis en cache — sinon tes
#    visiteurs continuent de voir l'ancienne version après un déploiement.
# ------------------------------------------------------------------
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 month"

  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/avif "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"

  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"
</IfModule>

<IfModule mod_headers.c>
  # Fichiers hashés par Vite : immuables
  <FilesMatch "\.(js|css|woff2|webp|avif|svg|jpe?g|png|gif|ico)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # Le point d'entrée doit toujours être revalidé
  <FilesMatch "^index\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>

  # ----------------------------------------------------------------
  # 3. En-têtes de sécurité
  # ----------------------------------------------------------------
  Header set X-Content-Type-Options "nosniff"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Permissions-Policy "geolocation=(), microphone=(), camera=()"
  Header always unset X-Powered-By
</IfModule>

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

<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS text/html text/css text/plain
  AddOutputFilterByType BROTLI_COMPRESS application/javascript
  AddOutputFilterByType BROTLI_COMPRESS application/json image/svg+xml
</IfModule>

# ------------------------------------------------------------------
# 5. Types MIME et encodage
# ------------------------------------------------------------------
<IfModule mod_mime.c>
  AddType application/javascript .js .mjs
  AddType font/woff2 .woff2
  AddType image/webp .webp
  AddType image/avif .avif
  AddType application/manifest+json .webmanifest
  AddDefaultCharset UTF-8
</IfModule>

# ------------------------------------------------------------------
# 6. Divers
# ------------------------------------------------------------------
Options -Indexes

<IfModule mod_autoindex.c>
  IndexIgnore *
</IfModule>

<FilesMatch "^\.(env|git|htaccess|htpasswd)">
  Require all denied
</FilesMatch>
