Options -Indexes
RewriteEngine On
RewriteBase /

# ── Protect .env file ──
<Files ".env">
    Require all denied
</Files>

# ── Security Headers ──
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=()"
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.googleapis.com *.bootstrapcdn.com *.cloudflare.com cdn.jsdelivr.net code.jquery.com plausible.io; style-src 'self' 'unsafe-inline' *.googleapis.com *.bootstrapcdn.com *.cloudflare.com cdn.jsdelivr.net; font-src 'self' data: *.gstatic.com *.cloudflare.com *.bootstrapcdn.com; img-src 'self' data: blob: https:; connect-src 'self' https:; worker-src 'self' blob:; frame-ancestors 'self'; object-src 'none'; base-uri 'self';"
# HSTS: 1 an, includeSubDomains
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS

# Pass Authorization header to PHP (required for Bearer tokens in CGI/FastCGI)
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

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

# ── Cache statique ──
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css                  "access plus 1 month"
    ExpiresByType application/javascript    "access plus 1 month"
    ExpiresByType image/png                 "access plus 1 year"
    ExpiresByType image/jpeg                "access plus 1 year"
    ExpiresByType image/gif                 "access plus 1 year"
    ExpiresByType image/svg+xml             "access plus 1 year"
    ExpiresByType image/webp                "access plus 1 year"
    ExpiresByType font/woff2                "access plus 1 year"
    ExpiresByType font/woff                 "access plus 1 year"
</IfModule>

# Servir les fichiers statiques depuis public/ s'ils existent
RewriteCond %{REQUEST_URI} \.(css|js|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot|map|webp|pdf|txt|xml)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ public%{REQUEST_URI} [L]

# Tout le reste → public/index.php (front controller)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]
