# ─────────────────────────────────────────────────────────────
# Theme Dentista — .htaccess
# Optimizado para Core Web Vitals: caché, compresión y WebP
# ─────────────────────────────────────────────────────────────

# ── Compresión GZIP ──────────────────────────────────────────
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE font/woff2
    AddOutputFilterByType DEFLATE font/woff

    # Eliminar bugs con proxies
    <IfModule mod_setenvif.c>
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </IfModule>
</IfModule>

# ── Caché de assets estáticos ────────────────────────────────
# Los assets de Vite tienen hash en el nombre de archivo.
# Se pueden cachear indefinidamente (1 año).
<IfModule mod_expires.c>
    ExpiresActive On

    # Assets con hash — caché permanente
    <FilesMatch "\.[0-9a-f]{8,}\.(js|css|woff2|woff|webp|jpg|jpeg|png|gif|svg|ico)$">
        ExpiresDefault "access plus 1 year"
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # Assets sin hash — caché corta
    ExpiresByType image/webp              "access plus 1 month"
    ExpiresByType image/jpeg              "access plus 1 month"
    ExpiresByType image/png               "access plus 1 month"
    ExpiresByType image/gif               "access plus 1 month"
    ExpiresByType image/svg+xml           "access plus 1 month"
    ExpiresByType image/x-icon            "access plus 1 year"
    ExpiresByType font/woff2              "access plus 1 year"
    ExpiresByType font/woff               "access plus 1 year"
    ExpiresByType text/css                "access plus 1 week"
    ExpiresByType application/javascript  "access plus 1 week"

    # HTML — sin caché (WordPress genera páginas dinámicas)
    ExpiresByType text/html               "access plus 0 seconds"
</IfModule>

# ── Headers de seguridad básicos ─────────────────────────────
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    # Eliminar ETag (evita conflictos con Expires/Cache-Control)
    Header unset ETag
</IfModule>
FileETag None

# ── Servir WebP si el navegador lo soporta ───────────────────
<IfModule mod_rewrite.c>
    RewriteEngine On

    # WebP para JPEG/PNG: si existe la versión .webp y el navegador la acepta, servirla
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_FILENAME} \.(jpe?g|png)$
    RewriteCond %{REQUEST_FILENAME}\.webp -f
    RewriteRule ^(.+)\.(jpe?g|png)$ $1.$2.webp [T=image/webp,E=REQUEST_image_webp:1,L]

    # WordPress — dejar pasar requests normales
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

# ── Bloquear acceso a archivos sensibles ─────────────────────
<FilesMatch "(^\.htaccess|wp-config\.php|readme\.html|license\.txt|xmlrpc\.php)$">
    Require all denied
</FilesMatch>

# ── Desactivar listado de directorios ────────────────────────
Options -Indexes
