# ==============================================================
#  MIN 4 Sukabumi · CodeIgniter 4 (struktur cPanel: semua di public_html)
# ==============================================================

Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    # Jika situs berada di subfolder (mis. public_html/web), isi: RewriteBase /web/
    RewriteBase /

    # --- Paksa HTTPS (hapus tanda # setelah SSL aktif) ---
    # RewriteCond %{HTTPS} !=on
    # RewriteCond %{HTTP:X-Forwarded-Proto} !=https
    # RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # --- Paksa tanpa www (hapus tanda # bila diinginkan) ---
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    # RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

    # --- Blokir folder & file sistem dari akses publik ---
    RewriteRule ^(app|writable|vendor|database|tests|system)(/|$) - [F,L]
    RewriteRule ^(\.env.*|\.git.*|composer\.(json|lock)|spark|.*\.(sql|md|log|db|sqlite|bak|ini|sh))$ - [F,L,NC]

    # --- Hapus garis miring di akhir URL (SEO: satu URL kanonik) ---
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # --- index.php tidak tampil di URL ---
    RewriteCond %{THE_REQUEST} \s/+index\.php[\s?/] [NC]
    RewriteRule ^index\.php/?(.*)$ /$1 [L,R=301]

    # --- robots.txt & sitemap dibuat dinamis oleh aplikasi ---
    RewriteRule ^(robots\.txt|sitemap\.xml|feed\.xml)$ index.php/$1 [L]

    # --- Semua permintaan lain diarahkan ke CodeIgniter ---
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,NC,QSA]

    # Teruskan header Authorization
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>

# --- Lindungi file sensitif (lapis kedua) ---
<FilesMatch "^(\.env.*|\.htaccess|composer\.(json|lock)|spark|.*\.(sql|md|log|db|sqlite|bak))$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# --- Header keamanan ---
<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"
    Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
    # Aktifkan setelah HTTPS berjalan normal:
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    Header unset X-Powered-By
</IfModule>

# --- Kompresi (kecepatan = faktor SEO) ---
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/rss+xml application/javascript application/json image/svg+xml
</IfModule>

# --- Cache browser untuk aset statis ---
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 hour"
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/pdf "access plus 1 month"
</IfModule>

# --- Pengaturan PHP (abaikan bila host memakai PHP-FPM; atur via cPanel > MultiPHP INI Editor) ---
<IfModule mod_php.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 12M
    php_flag display_errors Off
    php_flag expose_php Off
</IfModule>
