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

    RewriteEngine On
    RewriteBase /

    # Keep the browser URL at the domain root, not /public or .php.
    RewriteCond %{THE_REQUEST} \s/+public/([^\s?]*) [NC]
    RewriteRule ^public/(.*)$ /$1 [R=301,L]

    RewriteCond %{THE_REQUEST} ^GET\s/+index\.php[\s?] [NC]
    RewriteRule ^index\.php$ / [R=301,L]

    RewriteCond %{THE_REQUEST} ^GET\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !^/storage/
    RewriteRule ^(app|bootstrap|config|database|resources|routes|storage|vendor|tests)(/|$) - [F,L]
    RewriteRule ^(artisan|composer\.(json|lock)|phpunit\.xml|HOSTING\.txt)$ - [F,L]
    RewriteRule (?:^|/)\.env - [F,L]

    RewriteCond %{REQUEST_URI} ^/storage/
    RewriteRule ^storage/(.*)$ public/index.php [L]

    RewriteCond %{REQUEST_URI} !^/public/
    RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI}.php -f
    RewriteRule ^(.+)$ public/$1.php [L]

    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

<Files ".env">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Deny from all
    </IfModule>
</Files>
