<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    # Prefer SPA over Laravel when the request is for a directory (e.g. /).
    DirectoryIndex index.html index.php

    # Apache 2.4+: pass Authorization to PHP-FPM/CGI (cPanel)
    CGIPassAuth On

    RewriteEngine On

    # Force HTTPS on production domain
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^support\.techronltd\.co\.ke$ [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Block hidden files (.env, .git, etc.)
    RewriteRule (^|/)\. - [F]

    # Pass Authorization to PHP (Apache + LiteSpeed on cPanel)
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Handle X-XSRF-Token Header
    RewriteCond %{HTTP:x-xsrf-token} .
    RewriteRule .* - [E=HTTP_X_XSRF_TOKEN:%{HTTP:X-XSRF-Token}]

    # Always serve the SPA at site root (LiteSpeed often skips DirectoryIndex).
    RewriteRule ^$ index.html [L]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Laravel API, webhooks, and health check
    RewriteCond %{REQUEST_URI} ^/(api|sanctum|up|webhooks)(/|$) [NC]
    RewriteRule ^ index.php [L]

    # Existing static files (built SPA assets, favicons, storage)
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    # Directory with an SPA index → serve it (covers / and nested dirs)
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME}/index.html -f
    RewriteRule ^(.*)$ $1/index.html [L]

    # React SPA client routes (/login, /admin, …)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [L]
</IfModule>
