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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Handle X-XSRF-Token Header
    RewriteCond %{HTTP:x-xsrf-token} .
    RewriteRule .* - [E=HTTP_X_XSRF_TOKEN:%{HTTP:X-XSRF-Token}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=308]

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

# ---------------------------------------------------------------------------
# Performance — see docs/PERFORMANCE.md. Every block is optional: without the
# module the site still works, only slower. (Nginx: mirror these in the vhost.)
# ---------------------------------------------------------------------------

# Compress text responses: Brotli where available, gzip otherwise.
<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml application/xml application/javascript application/json application/ld+json image/svg+xml
</IfModule>
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/javascript application/json application/ld+json image/svg+xml
</IfModule>

<IfModule mod_headers.c>
    # Other static files (icons, share image, uploaded logo and media): a week.
    # Media URLs carry ?v=<modified time>, so a replaced file is fetched anew.
    <FilesMatch "\.(ico|svg|png|jpe?g|webp|avif|woff2?|mp4|webm)$">
        Header set Cache-Control "public, max-age=604800"
    </FilesMatch>

    # Build files carry a content hash in their name: cache them for a year.
    <If "%{REQUEST_URI} =~ m#^/build/assets/#">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </If>

    # Uploaded SVGs (logos) are pictures and nothing else. The panel rejects
    # SVGs that contain scripts; should one slip through anyway, a browser that
    # opens the file directly must not run it.
    <If "%{REQUEST_URI} =~ m#^/storage/.*\.svg$#i">
        Header set Content-Security-Policy "default-src 'none'; style-src 'unsafe-inline'; sandbox"
        Header set X-Content-Type-Options "nosniff"
    </If>
</IfModule>
