# BEGIN WPMgr Cache
# GZIP compression for text files: HTML, CSS, JS, XML, fonts.
# This whole section is stripped on OpenLiteSpeed (LSWS_EDITION) where the
# .gz handling and mod_deflate no-gzip directives conflict.
<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
	AddOutputFilterByType DEFLATE text/javascript application/javascript application/json
	AddOutputFilterByType DEFLATE application/xml application/rss+xml
	AddOutputFilterByType DEFLATE font/woff2 font/ttf font/otf image/svg+xml
	# Never double-gzip an already-gzipped .html.gz cache file.
	SetEnvIfNoCase Request_URI \.gz$ no-gzip
</IfModule>
# End GZIP compression

# BEGIN WPMgr Browser Cache
<IfModule mod_expires.c>
	ExpiresActive On

	# Stylesheets & scripts
	ExpiresByType text/css                      "access plus 1 year"
	ExpiresByType application/javascript        "access plus 1 year"
	ExpiresByType text/javascript               "access plus 1 year"

	# Images
	ExpiresByType image/png                     "access plus 1 year"
	ExpiresByType image/jpeg                    "access plus 1 year"
	ExpiresByType image/gif                     "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 image/x-icon                  "access plus 1 year"

	# Fonts
	ExpiresByType font/woff2                    "access plus 1 year"
	ExpiresByType font/woff                     "access plus 1 year"
	ExpiresByType application/font-woff2        "access plus 1 year"
	ExpiresByType application/vnd.ms-fontobject "access plus 1 year"

	# Media & documents
	ExpiresByType video/mp4                     "access plus 1 year"
	ExpiresByType application/pdf               "access plus 1 month"
</IfModule>

<IfModule mod_headers.c>
	<FilesMatch "\.(css|js|mjs|woff2?|ttf|otf|eot|avif|webp|png|jpe?g|gif|svg|ico|mp4|webm|pdf)$">
		Header set Cache-Control "public, max-age=31536000"
	</FilesMatch>
</IfModule>
# END WPMgr Browser Cache

# Response headers for the cached .html.gz pages.
<FilesMatch "\.html\.gz$">
	<IfModule mod_headers.c>
		Header set Content-Encoding "gzip"
		Header set Content-Type "text/html; charset=UTF-8"
		Header set Cache-Tag "HOSTNAME"
		Header set CDN-Cache-Control "max-age=2592000"
		Header set Cache-Control "no-cache, must-revalidate"
		Header set x-wpmgr-cache "HIT"
		Header set x-wpmgr-source "Web Server"
	</IfModule>
	# Fallback for servers without mod_headers.
	<IfModule mod_mime.c>
		AddType text/html .gz
		AddCharset UTF-8 .gz
		AddEncoding gzip .gz
	</IfModule>
</FilesMatch>

# Serve cached pages straight from disk when present.
# CACHE_CONTENT_PATH is replaced at install time with the real WP_CONTENT_DIR-
# relative path (e.g. wp-content/cache/wpmgr) so this block is correct even
# when wp-content is relocated outside the webroot.
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /

	# Block direct hits on the cache directory.
	RewriteCond %{THE_REQUEST} "/CACHE_CONTENT_PATH/.*\.html\.gz" [NC]
	RewriteRule ^ - [F]

	# Mobile-caching flag. The HtaccessManager flips :0 to :1 when mobile
	# caching is enabled; the serve rules below gate on it.
	RewriteRule ^ - [E=MOBILE_CACHING_FLAG:0]

	# Serve the mobile cache for mobile UAs when mobile caching is enabled.
	RewriteCond %{REQUEST_METHOD} GET|HEAD
	RewriteCond %{QUERY_STRING} =""
	RewriteCond %{HTTP:Cookie} =""
	RewriteCond %{REQUEST_URI} !^/(wp-(?:admin|login|register|comments-post|cron|json))/ [NC]
	RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos" [NC]
	RewriteCond %{ENV:MOBILE_CACHING_FLAG} =1
	RewriteCond %{DOCUMENT_ROOT}/CACHE_CONTENT_PATH/%{HTTP_HOST}%{REQUEST_URI}/index-mobile.html.gz -f
	RewriteRule ^(.*)$ CACHE_CONTENT_PATH/%{HTTP_HOST}%{REQUEST_URI}/index-mobile.html.gz [L]

	# Serve the desktop cache otherwise.
	RewriteCond %{REQUEST_METHOD} GET|HEAD
	RewriteCond %{QUERY_STRING} =""
	RewriteCond %{HTTP:Cookie} =""
	RewriteCond %{REQUEST_URI} !^/(wp-(?:admin|login|register|comments-post|cron|json))/ [NC]
	RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos)" [NC,OR]
	RewriteCond %{ENV:MOBILE_CACHING_FLAG} !=1
	RewriteCond %{DOCUMENT_ROOT}/CACHE_CONTENT_PATH/%{HTTP_HOST}%{REQUEST_URI}/index.html.gz -f
	RewriteRule ^(.*)$ CACHE_CONTENT_PATH/%{HTTP_HOST}%{REQUEST_URI}/index.html.gz [L]
</IfModule>
# END WPMgr Cache
