# ============================================================
# eSurat PPM — Apache .htaccess
# cPanel Rumahweb Hosting
# ============================================================

Options -Indexes
ServerSignature Off

# ---- Fix: session.gc_divisor PHP Startup warning ----
php_value session.gc_divisor 100
php_value session.gc_probability 1
php_value session.gc_maxlifetime 1440

# ---- Blokir akses ke folder sensitif ----
<FilesMatch "\.(sql|log|env|json|lock|md|sh|ini|php\.bak)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ---- Proteksi folder includes ----
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /esurat/

    # PENTING: Teruskan Authorization header ke PHP
    # (shared hosting Apache sering strip header ini)
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Blokir akses langsung ke includes/
    RewriteRule ^includes/ - [F,L]

    # Blokir akses langsung ke uploads/ via URL (bukan rewrite block)
    RewriteRule ^uploads/.*\.php$ - [F,L]

    # API routing: /api/auth/login → /api/index.php?ep=auth/login
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^api/(.+)$ api/index.php?ep=$1 [QSA,L]

    # SPA frontend: semua non-file → index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/esurat/api/
    RewriteRule ^ index.html [L]
</IfModule>

# ---- Security headers ----
<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"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>

# ---- Gzip compression ----
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json
</IfModule>

# ---- Browser caching ----
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 7 days"
    ExpiresByType application/javascript "access plus 7 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
    ExpiresByType application/json "access plus 0 seconds"
</IfModule>
