# BEGIN ZiziCache

# 1. Browser caching: Expires headers
<IfModule mod_expires.c>
    ExpiresActive on
    # HTML, XML, JSON: no browser cache
    ExpiresByType text/html                     "access plus 0 seconds"
    ExpiresByType text/xml                      "access plus 0 seconds"
    ExpiresByType application/xml               "access plus 0 seconds"
    ExpiresByType application/json              "access plus 0 seconds"

    # Static assets: 1 year
    ExpiresByType image/x-icon                  "access plus 1 year"
    ExpiresByType image/gif                     "access plus 1 year"
    ExpiresByType image/png                     "access plus 1 year"
    ExpiresByType image/jpeg                    "access plus 1 year"
    ExpiresByType image/webp                    "access plus 1 year"
    ExpiresByType image/avif                    "access plus 1 year"
    ExpiresByType image/svg+xml                 "access plus 1 year"
    ExpiresByType video/ogg                     "access plus 1 year"
    ExpiresByType audio/ogg                     "access plus 1 year"
    ExpiresByType video/mp4                     "access plus 1 year"
    ExpiresByType video/webm                    "access plus 1 year"
    ExpiresByType text/css                      "access plus 1 year"
    ExpiresByType application/javascript        "access plus 1 year"
    ExpiresByType font/woff                     "access plus 1 year"
    ExpiresByType font/woff2                    "access plus 1 year"
    ExpiresByType font/ttf                      "access plus 1 year"
    ExpiresByType font/otf                      "access plus 1 year"
</IfModule>

# 2. GZIP compression
<IfModule mod_deflate.c>
    # Text, HTML, XML, JSON
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/json

    # CSS, JavaScript
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript

    # Fonts
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/woff
    AddOutputFilterByType DEFLATE font/woff2

    # SVG and icons
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon

    # Prevent double-compression
    SetEnvIfNoCase Request_URI \.gz$ no-gzip
</IfModule>

# 3. Headers for cached .html.gz (OLS server aggressive override)
<FilesMatch "\.html\.gz$">
    <IfModule mod_headers.c>
        # Aggressive header override for OpenLiteSpeed compatibility
        Header unset Content-Type
        Header unset Content-Disposition
        Header unset Content-Encoding
        
        # Force correct MIME type and encoding
        Header always set Content-Type "text/html; charset=UTF-8"
        Header always set Content-Encoding "gzip"
        Header always set Content-Disposition "inline"
        
        # Cache control headers
        Header set Cache-Tag "HOSTNAME"
        Header set CDN-Cache-Control "max-age=2592000"
        Header set Cache-Control "public, max-age=0, must-revalidate"
        Header append Vary "Accept-Encoding"
        Header set x-zizi-cache-status "HIT"
    </IfModule>
    <IfModule mod_mime.c>
        # Force MIME type for .gz files
        RemoveType .gz
        AddType text/html .gz
        AddCharset UTF-8 .gz
        AddEncoding gzip .gz
    </IfModule>
    # Environment variables for OLS server (conditional)
    <IfModule LiteSpeed>
        SetEnv force-response-1.0 1
        SetEnv nokeepalive 1
    </IfModule>
</FilesMatch>

# 4. Rewrite rules: serve cache via advanced-cache.php for proper headers
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Block direct access to .gz cache files (precise version)
    RewriteCond %{THE_REQUEST} "/wp-content/cache/zizi-cache/.*\.html\.gz" [NC]
    RewriteRule ^ - [F]

    # Only redirect to WordPress if no cache exists and not logged in
    # This allows normal WordPress handling while preserving cache functionality
    RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
    RewriteCond %{HTTP:Cookie} !wordpress_logged_in [NC]
    RewriteCond %{REQUEST_URI} !^/(wp-(?:admin|login|register|comments-post|cron|json))/ [NC]
    RewriteCond %{REQUEST_URI} !^/wp-content/ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Let WordPress handle request normally (advanced-cache.php will handle cache)
    RewriteRule ^(.*)$ /index.php [L]
</IfModule>

# END ZiziCache
