Options -Indexes -MultiViews
ServerSignature Off

<IfModule mod_dir.c>
    DirectorySlash Off
</IfModule>

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Pass Authorization header through to PHP on CGI / FastCGI / LiteSpeed
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

    # Block direct web access to internal PHP directories, hidden files, and private payment proofs
    RewriteRule ^(\.git|\.env|includes/|views/|storage/|uploads/payment-proofs/|cron/sync-sports\.php) - [F,L]

    # Block direct .php URLs in public paths (except installer and root index.php)
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteCond %{REQUEST_URI} !^/install/index\.php$ [NC]
    RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
    RewriteRule ^ - [R=404,L]

    # Remove trailing slash for canonical SEO consistency (except root)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [R=301,L]

    # Dynamic XML Sitemap
    RewriteRule ^sitemap\.xml$ sitemap.php [L,QSA]

    # Native PHP Sports Server HTTP Mirror (/php-server/...) — Protected by Internal Secret
    RewriteRule ^php-server/(.*)$ server.php?route=$1 [L,QSA]
    RewriteRule ^php-server/?$ server.php?route=events/count [L,QSA]

    # Centralized Sports REST API Gateway (/api/v1/...)
    RewriteRule ^api/v1/(.*)$ api/index.php?route=$1 [L,QSA]
    RewriteRule ^api/v1/?$ api/index.php?route= [L,QSA]

    # Serve existing static assets (CSS, JS, images, QR codes) directly
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Route all clean SEO URLs through Front Controller
    RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

# Block sensitive file extensions
<FilesMatch "\.(sql|log|ini|bak|env|sh|md|json|lock)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</FilesMatch>

# Compression & Static Caching
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 7 days"
    ExpiresByType application/javascript "access plus 7 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
    ExpiresByType image/webp "access plus 30 days"
</IfModule>
