<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=301]

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

# ---------------------------------------------------------------------------
# Kay's Notary Travel: compression + browser caching for static files.
# Every block is optional (IfModule); enable mod_deflate, mod_expires and
# mod_headers on the server to get the full benefit (see README).
# ---------------------------------------------------------------------------

# Default type for the web manifest.
AddType application/manifest+json .webmanifest

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml application/xml application/javascript text/javascript application/json application/ld+json application/manifest+json image/svg+xml
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/javascript text/javascript application/json application/ld+json application/manifest+json image/svg+xml
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 0 seconds"
    ExpiresByType image/webp "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/x-icon "access plus 7 days"
    ExpiresByType image/vnd.microsoft.icon "access plus 7 days"
    ExpiresByType application/manifest+json "access plus 7 days"
</IfModule>

<IfModule mod_headers.c>
    # Vite build output has content hashes in the file names: cache for a year, never revalidate.
    <If "%{REQUEST_URI} =~ m#/build/assets/#">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </If>
    # Images: URLs from the site carry ?v={content hash}, so they can be cached for a year;
    # requests without a version (e.g. hot-linked) get 30 days.
    <If "%{REQUEST_URI} =~ m#/images/# && %{QUERY_STRING} =~ /(^|&)v=/">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </If>
    <ElseIf "%{REQUEST_URI} =~ m#/images/#">
        Header set Cache-Control "public, max-age=2592000"
    </ElseIf>
    <FilesMatch "\.(ico|webmanifest)$">
        Header set Cache-Control "public, max-age=604800"
    </FilesMatch>
</IfModule>
