# Mencegah Directory Listing
Options -Indexes

# Menyembunyikan informasi error bawaan PHP ke publik
# php_flag display_errors off
# php_value error_reporting 0

# Halaman Error Kustom
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

# Melindungi file tersembunyi (dotfiles seperti .env, .htaccess, dll)
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# Melindungi file sensitif tertentu (termasuk config.php)
<FilesMatch "(?i)(\.(env|sql|log|bak|ini)|composer\.(json|lock)|config\.php)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# Mencegah akses langsung ke folder includes
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect 301 dari URL lama ke URL baru (Hanya GET)
    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{THE_REQUEST} \s/+index\.php[\s?] [NC]
    RewriteRule ^index\.php$ / [R=301,L]

    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{THE_REQUEST} \s/+(artikel|produk|tes|kontak|login|register|lupa-password|reset-password|premium|profile|faq|privacy-policy|terms|disclaimer)\.php[\s?] [NC]
    RewriteRule ^(artikel|produk|tes|kontak|login|register|lupa-password|reset-password|premium|profile|faq|privacy-policy|terms|disclaimer)\.php$ /$1 [R=301,L]

    # Mencegah akses langsung ke folder includes
    RewriteRule ^includes/ - [F,L]
    RewriteRule ^database/ - [F,L]

    # 1. URL Hasil Tes:
    RewriteRule ^hasil/([0-9]+)/([a-zA-Z0-9]+)/?$ hasil-tes.php?attempt_id=$1&token=$2 [QSA,L]
    RewriteRule ^hasil/([0-9]+)/?$ hasil-tes.php?attempt_id=$1 [QSA,L]

    # 2. URL Tes:
    RewriteRule ^tes/([a-zA-Z0-9-]+)/onboarding/?$ tes-onboarding.php?slug=$1 [QSA,L]
    RewriteRule ^tes/([a-zA-Z0-9-]+)/mulai/?$ mulai-tes.php?slug=$1 [QSA,L]
    RewriteRule ^tes/([a-zA-Z0-9-]+)/?$ tes-detail.php?slug=$1 [QSA,L]

    # 3. URL Produk:
    RewriteRule ^produk/([a-zA-Z0-9-]+)/?$ produk-detail.php?slug=$1 [QSA,L]

    # 4. Abaikan file dan folder asli (assets, admin folder, dll)
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Lupa Password routes
    RewriteRule ^lupa-password/?$ lupa-password.php [QSA,L]
    RewriteRule ^reset-password/?$ reset-password.php [QSA,L]

    # 5. Hapus ekstensi .php untuk URL sistem (/artikel, /produk, /admin/dashboard, dll)
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([a-zA-Z0-9_/-]+)/?$ $1.php [L]

    # 6. Slug Dinamis untuk Artikel dan Halaman Statis (/slug)
    # Akan diteruskan ke resolve-slug.php.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9-]+)/?$ resolve-slug.php?slug=$1 [QSA,L]

</IfModule>
