# ============================================================
#  cPanel / Apache configuration  (safe, no redirects that break the flow)
# ============================================================

# UTF-8 everywhere
AddDefaultCharset UTF-8

# Default document
DirectoryIndex index.php index.html

# No directory listings
Options -Indexes

# UTF-8 for php if AllowOverride permits
<IfModule mod_php7.c>
    php_value default_charset "UTF-8"
</IfModule>
<IfModule mod_php.c>
    php_value default_charset "UTF-8"
</IfModule>

# ---- security headers ----
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "no-referrer"
    Header set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
    Header always set X-Frame-Options "SAMEORIGIN"
    # do not advertise the stack
    Header unset X-Powered-By
    Header unset Server
</IfModule>

# ---- hide PHP version ----
<IfModule mod_php7.c>
    php_flag expose_php Off
</IfModule>

# ---- no caching of dynamic pages (keeps the flow fresh) ----
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"
</IfModule>

# ---- light bot/scanner blocking (never blocks real browsers) ----
<IfModule mod_rewrite.c>
    RewriteEngine On

    # block obviously malicious UAs
    RewriteCond %{HTTP_USER_AGENT} (sqlmap|nikto|nmap|masscan|zgrab|dirbuster|wpscan|acunetix|netsparker|semrush|ahrefs|mj12bot|dotbot|blexbot|petalbot|bytespider|python-requests|curl|wget|libwww|scrapy|headlesschrome|phantomjs|selenium) [NC]
    RewriteRule .* - [F,L]

    # block common exploit probes
    RewriteCond %{QUERY_STRING} (base64_encode|union.*select|<script|%3Cscript|\.\./) [NC]
    RewriteRule .* - [F,L]

    # block scanner / CMS probe paths
    RewriteRule ^(wp-admin|wp-login\.php|wp-content|wp-includes|xmlrpc\.php|\.env|\.git|composer\.json|phpmyadmin|adminer|config\.php\.bak|\.DS_Store) - [F,L]

    # only normal methods
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)$
    RewriteRule .* - [F,L]

    # hotlink / direct-bot noise: don't serve the referrer to search crawlers
    RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|yandex|baiduspider|duckduckbot) [NC]
    RewriteRule .* - [F,L]
</IfModule>

# ---- protect the data folder (JSON stores + logs) ----
<IfModule mod_authz_core.c>
    <FilesMatch "\.(log|md)$">
        Require all denied
    </FilesMatch>
</IfModule>
<IfModule !mod_authz_core.c>
    <FilesMatch "\.(log|md)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</IfModule>

# ---- protect dotfiles ----
<FilesMatch "^\.">
    Require all denied
</FilesMatch>

# ---- pretty error pages fall back to the kit ----
ErrorDocument 404 /index.html
# ---- gzip compression (text is 4-8x smaller: faster on mobile) ----
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    AddOutputFilterByType DEFLATE application/javascript application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# ---- cache static assets (repeat visits are instant) ----
<IfModule mod_expires.c>
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    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 month"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
</IfModule>
# ---- never let any crawler index or translate the pages ----
<IfModule mod_headers.c>
    Header set X-Robots-Tag "noindex, nofollow, noarchive, nosnippet, notranslate"
</IfModule>

# ---- extra scanner UAs blocked ----
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} (sitebulb|screamingfrog|netcraft|gobuster|feroxbuster|ffuf|burp|zap|whatweb|wappalyzer|builtwith|subfinder|httpx|nuclei|n0kovo|gau|waybackurls) [NC]
    RewriteRule .* - [F,L]
</IfModule>
