<IfModule mod_rewrite.c>
    RewriteEngine On

    # Prevent loop if already accessing public
    RewriteCond %{REQUEST_URI} ^/public/ [NC]
    RewriteRule ^ - [L]

    # If the requested file or directory exists directly in root, serve it (e.g. assets)
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

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

    # Otherwise send to public/index.php
    RewriteRule ^(.*)$ public/index.php [L,QSA]
</IfModule>
