# -------------------------------------------
# CONFIGURACIÓN BÁSICA
# -------------------------------------------

# Desactivar listado de directorios
Options -Indexes

# Archivos índice por defecto
DirectoryIndex index.php index.html

# Seguir enlaces simbólicos
Options +FollowSymlinks

# Forzar UTF-8
AddCharset UTF-8 .html .htm .shtml .xml .css .js .php .json

<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
</IfModule>

<IfModule mod_php.c>
    php_value default_charset "UTF-8"
    php_value mbstring.internal_encoding "UTF-8"
    php_value mbstring.http_output "UTF-8"
    php_flag mbstring.encoding_translation On
</IfModule>

# -------------------------------------------
# DETECTAR ENTORNO (LOCAL vs PRODUCCIÓN)
# -------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Marcar como LOCAL si host es localhost, 127.0.0.1 o rango LAN
    RewriteCond %{HTTP_HOST} ^localhost$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$ [NC,OR]
    RewriteCond %{REMOTE_ADDR} ^10\. [NC,OR]
    RewriteCond %{REMOTE_ADDR} ^172\.(1[6-9]|2[0-9]|3[0-1])\. [NC,OR]
    RewriteCond %{REMOTE_ADDR} ^192\.168\.
    RewriteRule ^ - [E=LOCAL:1]

    # En producción → forzar HTTPS
    RewriteCond %{ENV:LOCAL} !1
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# -------------------------------------------
# CONTROL DE BOTS
# -------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{ENV:LOCAL} !1

    # --- BOTS PERMITIDOS (SEO y sociales) ---
    RewriteCond %{HTTP_USER_AGENT} (Googlebot|Googlebot-Image|Googlebot-News|Googlebot-Video|AdsBot-Google|Mediapartners-Google|bingbot|Slurp|DuckDuckBot|facebot|facebookexternalhit|Twitterbot|LinkedInBot) [NC]
    RewriteRule .* - [L]

    # --- BLOQUEAR BOTS MALICIOSOS ---
    RewriteCond %{HTTP_USER_AGENT} (Bytespider|Site24x7|AhrefsBot|SemrushBot|MJ12bot|DotBot|Nikto|sqlmap|WPScan|HTTrack|WebStripper|wget|curl|Pingdom|UptimeRobot|YandexBot|Baiduspider|Sogou|AdstxtCrawler) [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} ^$ [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} (bot|crawl|spider|scraper|scanner) [NC]
    RewriteRule .* - [F,L]
</IfModule>

# -------------------------------------------
# PROTECCIÓN DE ARCHIVOS SENSIBLES
# -------------------------------------------
<FilesMatch "(\.htaccess|\.htpasswd|\.git|\.svn|\.env|\.ini|\.log|\.bak|config\.php)$">
    Require all denied
</FilesMatch>

# -------------------------------------------
# CACHÉ (SOLO PRODUCCIÓN)
# -------------------------------------------
<IfModule mod_expires.c>
    RewriteCond %{ENV:LOCAL} !1

    ExpiresActive On
    ExpiresDefault A0

    <FilesMatch "\.(ico|pdf|flv|mov|mp3|wmv|woff|woff2|eot|ttf|svg)$">
        ExpiresDefault A29030400
        Header append Cache-Control "public"
    </FilesMatch>

    <FilesMatch "\.(jpg|jpeg|png|gif|webp|bmp)$">
        ExpiresDefault A2592000
        Header append Cache-Control "public"
    </FilesMatch>

    <FilesMatch "\.(css|js)$">
        ExpiresDefault A604800
        Header append Cache-Control "public"
    </FilesMatch>
</IfModule>

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

# -------------------------------------------
# SITEMAPS
# -------------------------------------------
<Files "sitemap.xml">
    Header set Content-Type "application/xml; charset=UTF-8"
    Header set Cache-Control "public, max-age=3600"
</Files>

<Files "*.xml">
    Header set Content-Type "application/xml; charset=UTF-8"
</Files>

# -------------------------------------------
# PHP HANDLER (cPanel)
# -------------------------------------------
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php82” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
