# ================================================================
# VirdaPay — Konfigurasi Apache untuk cPanel / Shared Hosting
# ================================================================
# Upload file INI bersama seluruh isi dist/ ke folder public_html.
# Pastikan "Show Hidden Files" diaktifkan di cPanel File Manager
# agar file .htaccess (berawalan titik) terlihat dan terupload.
# ================================================================

# ----------------------------------------------------------------
# 0) REDIRECT HTTP → HTTPS (aktifkan jika hosting sudah punya SSL)
#    Hapus tanda # di bawah ini jika domain sudah menggunakan HTTPS
# ----------------------------------------------------------------
# <IfModule mod_rewrite.c>
#   RewriteEngine On
#   RewriteCond %{HTTPS} off
#   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# </IfModule>

# ----------------------------------------------------------------
# 1) SPA ROUTING — Tanpa ini, refresh di /layanan atau /founder
#    akan menghasilkan error 404 dari Apache. Semua request
#    diarahkan ke index.html agar React Router yang menangani.
# ----------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # Jika yang diminta adalah file atau folder yang benar-benar ada,
  # sajikan apa adanya (gambar, CSS, JS, dll.)
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Semua request lain → index.html (React Router yang handle)
  RewriteRule ^ index.html [L]
</IfModule>

# ----------------------------------------------------------------
# 2) GZIP COMPRESSION — Memperkecil ukuran file yang dikirim
# ----------------------------------------------------------------
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE font/woff2
  AddOutputFilterByType DEFLATE font/woff
</IfModule>

# ----------------------------------------------------------------
# 3) BROWSER CACHING
#    Asset Vite sudah punya hash di nama file → aman cache lama.
#    index.html tidak di-cache agar update selalu terbaca.
# ----------------------------------------------------------------
<IfModule mod_expires.c>
  ExpiresActive On

  # HTML — jangan cache (selalu ambil versi terbaru)
  ExpiresByType text/html                "access plus 0 seconds"

  # CSS & JS — 1 tahun (aman karena ada hash di nama file)
  ExpiresByType text/css                 "access plus 1 year"
  ExpiresByType application/javascript   "access plus 1 year"
  ExpiresByType text/javascript          "access plus 1 year"

  # Gambar & font
  ExpiresByType image/png                "access plus 1 year"
  ExpiresByType image/jpeg               "access plus 1 year"
  ExpiresByType image/gif                "access plus 1 year"
  ExpiresByType image/webp               "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 font/woff                "access plus 1 year"
  ExpiresByType application/font-woff2   "access plus 1 year"
</IfModule>

# Header cache-control tambahan
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|png|jpg|jpeg|gif|webp|svg|woff2|woff|ico)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "\.(html)$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>
</IfModule>

# ----------------------------------------------------------------
# 4) SECURITY HEADERS — Penting untuk keamanan website
# ----------------------------------------------------------------
<IfModule mod_headers.c>
  # Cegah browser menebak tipe konten (MIME sniffing)
  Header always set X-Content-Type-Options "nosniff"

  # Proteksi XSS di browser lama
  Header always set X-XSS-Protection "1; mode=block"

  # Cegah website ditampilkan dalam iframe (clickjacking)
  Header always set X-Frame-Options "SAMEORIGIN"

  # Kontrol referrer yang dikirim ke situs lain
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ----------------------------------------------------------------
# 5) Cegah listing isi folder
# ----------------------------------------------------------------
Options -Indexes

# ----------------------------------------------------------------
# 6) Blokir akses langsung ke file sensitif
# ----------------------------------------------------------------
<FilesMatch "\.(env|log|sql|bak|sh|config)$">
  Order allow,deny
  Deny from all
</FilesMatch>
