# ==============================================================
# KPK Board Class 11-12 Textbooks & LMS
# Apache Configuration for Shared Hosting (cPanel, Hostinger, etc.)
# ==============================================================

DirectoryIndex index.html index.php

# No folder listings (e.g. /lms/install/)
Options -Indexes

# Security headers
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "same-origin"
</IfModule>

# Pass the login token header through to PHP on CGI/FastCGI hosts
<IfModule mod_setenvif.c>
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Prevent infinite loop if file/directory exists
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Route /api/... to the LMS API
    RewriteRule ^api/(.*)$ lms/api.php?route=$1 [QSA,L]

    # Clean URL redirect for /lms
    RewriteRule ^lms$ lms/ [R=301,L]
</IfModule>

# Ensure proper MIME types for modern WebP pages and fonts
<IfModule mod_mime.c>
    AddType image/webp .webp
    AddType application/json .json
    AddType font/woff2 .woff2
    AddType application/wasm .wasm
</IfModule>

# Caching for high-definition textbook scans to save bandwidth & increase speed
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
</IfModule>

# Local build tools and source PDFs are not part of the site
<IfModule mod_alias.c>
    RedirectMatch 404 ^/(.*/)?(_dev_python|math)(/|$)
</IfModule>

# Security: Protect SQLite databases and backups, SQL dumps, setup lock and notes from download
<FilesMatch "(\.db(-wal|-shm|-journal)?|\.py|\.sqlite|\.log|\.sql|\.md|\.lock)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order deny,allow
        Deny from all
    </IfModule>
</FilesMatch>

# Optional Gzip compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json application/javascript
</IfModule>
