<IfModule mod_rewrite.c>
    RewriteEngine On

    # Deny direct access to .env and sensitive files
    RewriteRule ^\.env - [F,L,NC]
    RewriteRule ^storage/database - [F,L,NC]

    # If requested file or directory exists directly in root, serve it
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # If file exists in public/ directory, rewrite directly to public/
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -d
    RewriteRule ^(.*)$ public/$1 [L]

    # Send all other requests to public/index.php
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/index.php [L]
</IfModule>
