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

    RewriteEngine On

    # --- Security: never serve app code, secrets, dumps, logs or scripts directly ---
    # The Laravel app lives in /project (with .env, vendor, logs, debug scripts) and
    # /storage; only /assets and /installer (static files) are meant to be public.
    RewriteRule ^(project|storage|bootstrap)(/|$) - [F,L,NC]
    # Dotfiles (.env, .git, ...) anywhere
    RewriteRule (^|/)\.(?!well-known/) - [F,L]
    # Database dumps, archives, logs, backups, composer files
    RewriteRule \.(sql|sql\.gz|zip|tar|gz|rar|7z|log|bak|old|orig|env|lock)$ - [F,L,NC]
    RewriteRule (^|/)(error_log|backup\.txt|composer\.json|composer\.lock|package\.json|artisan)$ - [F,L,NC]
    # CLI-only scripts in the web root
    RewriteRule ^deploy\.php$ - [F,L,NC]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# Fallback in case mod_rewrite is unavailable
<FilesMatch "(^\.|\.(sql|zip|log|bak|env)$|^(error_log|deploy\.php|backup\.txt)$)">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>
