# ==========================================
# 1. SEGURIDAD BÁSICA Y PROTECCIÓN DE ARCHIVOS
# ==========================================
Options -Indexes

<FilesMatch "\.(env|log|sql|json|ini|config|bak|sh|yml|yaml)$">
    Require all denied
</FilesMatch>

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} (scrapy|curl|wget) [NC]
RewriteRule .* - [F,L]

# ==========================================
# 2. SEO: REDIRECCIÓN HTTPS Y WWW OBLIGATORIO
# ==========================================
RewriteCond %{HTTP_HOST} ^cliente-fantasma\.com [NC]
RewriteRule ^(.*)$ https://www.cliente-fantasma.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

# ==========================================
# 3. VELOCIDAD: COMPRESIÓN GZIP
# ==========================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript image/svg+xml font/woff2 font/woff
</IfModule>

# ==========================================
# 4. VELOCIDAD: CACHÉ (1 SEMANA)
# ==========================================
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Imágenes, Íconos y Fuentes (1 semana)
    ExpiresByType image/jpg "access plus 1 week"
    ExpiresByType image/jpeg "access plus 1 week"
    ExpiresByType image/gif "access plus 1 week"
    ExpiresByType image/png "access plus 1 week"
    ExpiresByType image/webp "access plus 1 week"
    ExpiresByType image/svg+xml "access plus 1 week"
    ExpiresByType image/x-icon "access plus 1 week"
    
    # CSS y JS (1 semana)
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    
    # HTML (0 segundos - Siempre se descarga la versión más reciente)
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# ==========================================
# 5. CABECERAS DE SEGURIDAD HTTP
# ==========================================
<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
