Options -Indexes
DirectoryIndex index.php

# Custom Error Pages (Prevent raw webserver default error screens)
ErrorDocument 400 /error.php?code=400
ErrorDocument 401 /error.php?code=401
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php
ErrorDocument 502 /error.php?code=502
ErrorDocument 503 /error.php?code=503

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 1. Redirect /index.php or /index.html directly to /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.(php|html)[\s?] [NC]
    RewriteRule ^index\.(php|html)$ / [R=301,L]

    # 2. Redirect /sub/index.php or /sub/index.html to /sub/
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)/index\.(php|html)[\s?] [NC]
    RewriteRule ^(.*)/index\.(php|html)$ /$1/ [R=301,L]

    # 3. Redirect legacy Indonesian URLs to English canonical URLs (301 permanent redirect)
    RewriteRule ^baca(?:\.php)?$ /read [R=301,L]
    RewriteRule ^artikel(?:\.php)?$ /articles [R=301,L]
    RewriteRule ^baca-artikel(?:\.php)?$ /article [R=301,L]
    RewriteRule ^cari(?:\.php)?$ /search [R=301,L]
    RewriteRule ^diskusi(?:\.php)?$ /discussions [R=301,L]
    RewriteRule ^favorit(?:\.php)?$ /favorites [R=301,L]
    RewriteRule ^profil(?:\.php)?$ /profile [R=301,L]
    RewriteRule ^tokoh(?:-alkitab)?(?:\.php)?$ /characters [R=301,L]
    RewriteRule ^peristiwa(?:-alkitab)?(?:\.php)?$ /events [R=301,L]

    # 4. Redirect direct requests for *.php to extensionless URL (e.g. /read.php -> /read)
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^?\s]+)\.php[\s?] [NC]
    RewriteRule ^(.*)\.php$ /$1 [R=301,L]

    # 5. Block direct web access to internal directories (returns 403)
    RewriteRule ^(config|includes|database|src|node_modules)(/.*)?$ - [F,L]

    # 6. Serve existing files or directories as-is
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # 7. Rewrite extensionless request to .php if file exists
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)$ $1.php [L,QSA]

    # 8. Route non-existent files and directories to custom 404 handler
    RewriteRule ^.*$ 404.php [L]
</IfModule>

<FilesMatch "(^\.|\.(sql|json|lock|yaml|yml|md)$|Dockerfile)">
    Require all denied
</FilesMatch>

<IfModule mod_headers.c>
    Header unset X-Powered-By
    Header always unset X-Powered-By
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

<IfModule php_module>
    php_value upload_max_filesize 32M
    php_value post_max_size 36M
    php_value memory_limit 256M
    php_value max_execution_time 180
</IfModule>
