# ==============================================================
# .htaccess — unhar.ac.id (HARDENED)
# Letakkan file ini di: public_html/.htaccess
# BACKUP file .htaccess lama Anda sebelum mengganti dengan ini!
# ==============================================================

# --------------------------------------------------------------
# BAGIAN 1: WORDPRESS STANDARD REWRITE (JANGAN DIUBAH)
# Ini adalah kode default WordPress — wajib ada agar URL bisa berjalan
# --------------------------------------------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# --------------------------------------------------------------
# BAGIAN 2: PAKSA HTTPS (redirect semua HTTP ke HTTPS)
# Pastikan SSL sudah aktif di cPanel sebelum mengaktifkan ini
# --------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# --------------------------------------------------------------
# BAGIAN 3: SECURITY HEADERS
# Memberitahu browser cara melindungi pengunjung website
# --------------------------------------------------------------
<IfModule mod_headers.c>
    # Cegah website diload di dalam iframe oleh situs lain (anti-clickjacking)
    Header always set X-Frame-Options "SAMEORIGIN"

    # Aktifkan filter XSS bawaan browser
    Header always set X-XSS-Protection "1; mode=block"

    # Cegah browser menebak-nebak tipe file (anti MIME-sniffing)
    Header always set X-Content-Type-Options "nosniff"

    # Kontrol informasi referrer yang dikirim ke situs lain
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    # Nonaktifkan fitur browser yang tidak diperlukan
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"

    # Hapus header yang menampilkan versi server (sembunyikan info teknis)
    Header always unset X-Powered-By
    Header always unset Server
</IfModule>

# --------------------------------------------------------------
# BAGIAN 4: PROTEKSI FILE SENSITIF
# Blokir akses langsung ke file-file konfigurasi penting
# --------------------------------------------------------------
<FilesMatch "^(wp-config\.php|\.htaccess|\.htpasswd|readme\.html|license\.txt|wp-config-sample\.php|install\.php)$">
    Order Deny,Allow
    Deny from all
</FilesMatch>

# Blokir akses ke file log
<FilesMatch "\.(log|sql|bak|backup|gz|tar|zip)$">
    Order Deny,Allow
    Deny from all
</FilesMatch>

# --------------------------------------------------------------
# BAGIAN 5: NONAKTIFKAN DIRECTORY LISTING
# Cegah pengunjung melihat isi folder jika tidak ada index.php/html
# --------------------------------------------------------------
Options -Indexes

# --------------------------------------------------------------
# BAGIAN 6: PROTEKSI WP-LOGIN.PHP (halaman login WordPress)
# Ganti dengan IP kantor/rumah Anda. Jika IP tidak statis,
# hapus bagian ini dan gunakan plugin "Limit Login Attempts" saja.
# Cara cek IP Anda: https://whatismyip.com
# --------------------------------------------------------------
# <Files wp-login.php>
#     Order Deny,Allow
#     Deny from all
#     Allow from 203.175.9.138    <- ganti dengan IP Anda
# </Files>
# CATATAN: Bagian ini dikomentari agar tidak mengunci Anda sendiri.
# Aktifkan dengan menghapus tanda # setelah Anda tahu IP statis Anda.

# --------------------------------------------------------------
# BAGIAN 7: BLOKIR XMLRPC.PHP
# File ini sering dieksploitasi untuk brute force & DDoS
# Jika menggunakan Jetpack atau aplikasi mobile WordPress, JANGAN aktifkan
# --------------------------------------------------------------
<Files xmlrpc.php>
    Order Deny,Allow
    Deny from all
</Files>

# --------------------------------------------------------------
# BAGIAN 8: CEGAH EKSEKUSI PHP DI FOLDER UPLOADS
# Hacker sering upload file PHP berbahaya di folder ini
# --------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^wp-content/uploads/.*\.php$ - [F,L,NC]
RewriteRule ^wp-content/uploads/.*\.php5$ - [F,L,NC]
RewriteRule ^wp-content/uploads/.*\.phtml$ - [F,L,NC]
</IfModule>

# --------------------------------------------------------------
# BAGIAN 9: BLOKIR USER AGENT BERBAHAYA
# Menolak request dari tools scanning/hacking yang dikenal
# --------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python-requests|python-urllib|nikto|sqlmap|nmap|masscan|zgrab|nuclei|dirbuster|gobuster) [NC]
RewriteRule .* - [F,L]
</IfModule>

# --------------------------------------------------------------
# BAGIAN 10: PROTEKSI AKSES KE WP-INCLUDES
# Blokir akses PHP langsung ke wp-includes dari luar
# --------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>

# --------------------------------------------------------------
# BAGIAN 11: CEGAH HOTLINKING GAMBAR
# Cegah website lain menggunakan bandwidth Anda untuk menampilkan gambar
# --------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?unhar\.ac\.id [NC]
RewriteCond %{HTTP_REFERER} !^https://(ftk|feb|fbk|fh|lpmpi|lppm)\.unhar\.ac\.id [NC]
RewriteRule \.(jpg|jpeg|png|gif|webp|svg)$ - [F,NC,L]
</IfModule>

# --------------------------------------------------------------
# BAGIAN 12: CACHE CONTROL — Percepat loading website
# Mengatur browser menyimpan file statis agar tidak download ulang
# --------------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/x-font-woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

# Kompres file sebelum dikirim ke browser (percepat loading)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/xml application/xhtml+xml
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
