# BEGIN PlugoSpeed
# ===== Browser Caching =====
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html              "access plus 0 seconds"
    ExpiresByType text/css               "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType image/jpeg             "access plus 1 year"
    ExpiresByType image/png              "access plus 1 year"
    ExpiresByType image/webp             "access plus 1 year"
    ExpiresByType image/svg+xml          "access plus 1 year"
    ExpiresByType font/woff2             "access plus 1 year"
</IfModule>

# ===== GZIP for live (non-cached) content =====
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript
    # Don't double-compress pre-compressed .gz cache files.
    SetEnvIfNoCase Request_URI \.gz$ no-gzip dont-vary
</IfModule>

# ===== Security Headers + Pre-compressed Cache Headers =====
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"

    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|svg|ico|woff|woff2)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # Fix: tell browsers that .html.gz files are gzip-encoded HTML.
    # Without this, the browser downloads the raw gzip binary.
    <FilesMatch "\.html\.gz$">
        Header set Content-Encoding "gzip"
        Header set Content-Type "text/html; charset=UTF-8"
        Header set Cache-Control "no-cache, must-revalidate"
    </FilesMatch>
</IfModule>

# ===== Static HTML Cache Serving =====
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # Skip WP system paths
    RewriteRule ^wp-(admin|login|cron)/ - [L]
    RewriteRule ^xmlrpc\.php$ - [L]
    # Skip logged-in users
    RewriteCond %{HTTP_COOKIE} wordpress_logged_in_ [NC]
    RewriteRule .* - [E=PLUGOSPEED_SKIP:1]
    # Skip POST
    RewriteCond %{REQUEST_METHOD} POST
    RewriteRule .* - [E=PLUGOSPEED_SKIP:1]
    # Skip unknown query strings
    RewriteCond %{QUERY_STRING} !^(lang|currency)=[^&]+$
    RewriteCond %{QUERY_STRING} .+
    RewriteRule .* - [E=PLUGOSPEED_SKIP:1]

    # Serve homepage cache (REQUEST_URI = /)
    RewriteCond %{ENV:PLUGOSPEED_SKIP} !1
    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/plugospeed/%{HTTP_HOST}/default/index.html.gz -f
    RewriteRule ^$ /wp-content/cache/plugospeed/%{HTTP_HOST}/default/index.html.gz [L]

    RewriteCond %{ENV:PLUGOSPEED_SKIP} !1
    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/plugospeed/%{HTTP_HOST}/default/index.html -f
    RewriteRule ^$ /wp-content/cache/plugospeed/%{HTTP_HOST}/default/index.html [L]

    # Serve pre-compressed gzip cache if browser supports it.
    RewriteCond %{ENV:PLUGOSPEED_SKIP} !1
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/plugospeed/%{HTTP_HOST}/%{REQUEST_URI}/default/index.html.gz -f
    RewriteRule ^(.*)$ /wp-content/cache/plugospeed/%{HTTP_HOST}/%{REQUEST_URI}/default/index.html.gz [L]
    # Fallback: serve uncompressed HTML cache.
    RewriteCond %{ENV:PLUGOSPEED_SKIP} !1
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/plugospeed/%{HTTP_HOST}/%{REQUEST_URI}/default/index.html -f
    RewriteRule ^(.*)$ /wp-content/cache/plugospeed/%{HTTP_HOST}/%{REQUEST_URI}/default/index.html [L]
</IfModule>
# END PlugoSpeed
