# ══════════════════════════════════════════════════════════════════════════════
# .htaccess — Informe Semanal Teams · M&G Defontana
# Instalar en la misma carpeta que informe_teams.html, api.php
# ══════════════════════════════════════════════════════════════════════════════

# ── Seguridad básica ──────────────────────────────────────────────────────────
Options -Indexes                    # No listar directorios
ServerSignature Off                 # Ocultar versión del servidor

# ── Proteger archivos sensibles ───────────────────────────────────────────────

# Bloquear acceso directo a los archivos JSON de datos
<FilesMatch "^last_report\.(json|json\.bak)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Bloquear acceso directo al .htaccess y archivos de configuración
<FilesMatch "^(\.htaccess|\.htpasswd|\.env|composer\.(json|lock))$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# ── PHP: configuración de seguridad ──────────────────────────────────────────
<IfModule mod_php.c>
    php_flag  display_errors        Off
    php_flag  log_errors            On
    php_value error_log             /tmp/php_informe_teams_errors.log
    php_value upload_max_filesize   10M
    php_value post_max_size         10M
    php_value memory_limit          64M
    php_value max_execution_time    30
</IfModule>

# ── MIME types ────────────────────────────────────────────────────────────────
<IfModule mod_mime.c>
    AddType application/json        .json
    AddType text/html               .html
    AddCharset UTF-8                .html .php .json
</IfModule>

# ── Headers de seguridad ──────────────────────────────────────────────────────
<IfModule mod_headers.c>
    Header always set X-Frame-Options           "SAMEORIGIN"
    Header always set X-Content-Type-Options    "nosniff"
    Header always set X-XSS-Protection          "1; mode=block"
    Header always set Referrer-Policy           "strict-origin-when-cross-origin"
    # Cache: HTML sin caché, assets estáticos con caché larga
    <FilesMatch "\.(html|php)$">
        Header set Cache-Control "no-store, no-cache, must-revalidate"
        Header set Pragma "no-cache"
    </FilesMatch>
</IfModule>

# ── Rewrite rules ─────────────────────────────────────────────────────────────
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirigir HTTP → HTTPS (descomenta si tienes SSL)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Servir informe_teams.html como página principal (index)
    RewriteRule ^$ informe_teams.html [L]

    # Bloquear acceso directo a last_report.json desde URL (doble seguridad)
    RewriteRule ^last_report\.json(\.bak)?$ - [F,L]
</IfModule>

# ── Compresión GZIP (mejora rendimiento) ─────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain application/json application/javascript text/css
</IfModule>

# ── Página de error personalizada (opcional) ──────────────────────────────────
# ErrorDocument 403 /informe_teams.html
# ErrorDocument 404 /informe_teams.html
