<IfModule mod_rewrite.c>
    RewriteEngine On

    # Always redirect visible /public URLs to clean URLs.
    RewriteCond %{THE_REQUEST} \s/+public/?([^\s?]*) [NC]
    RewriteRule ^public/?(.*)$ /$1 [R=301,L,NE]

    # Serve Laravel from the public folder without showing /public in the browser.
    RewriteCond %{REQUEST_URI} !^/public/ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/$1 [L]

    # Allow direct access to real files/folders inside public as clean URLs.
    RewriteCond %{REQUEST_URI} !^/public/ [NC]
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
