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

    RewriteEngine On

    # Uploaded files are user content: never execute them as scripts
    RewriteRule ^storage/.*\.(php[0-9]?|phtml|phar|pht|cgi|pl|py|sh|asp|aspx|jsp|htaccess)$ - [F,NC,L]

    # 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]

    # Route resize requests for storage images through the app
    RewriteCond %{QUERY_STRING} (^|&)(w|h|q)=
    RewriteRule ^storage/(.+)$ index.php [L,QSA]

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

# Uploaded files must not be sniffed into another type, and documents (SVG/HTML/XML) open in an empty sandbox
# (not applied to PDFs/video: browsers cannot render those inside a CSP sandbox)
<IfModule mod_headers.c>
    <If "%{REQUEST_URI} =~ m#^/(.+/)?storage/#">
        Header always set X-Content-Type-Options "nosniff"
    </If>
    <If "%{REQUEST_URI} =~ m#^/(.+/)?storage/.+\.(svgz?|xml|x?html?)$#i">
        Header always set Content-Security-Policy "sandbox; default-src 'none'; img-src data:; style-src 'unsafe-inline'"
    </If>
</IfModule>
