#### BEGIN atec-webp
# DO NOT EDIT THESE LINES

<IfModule mod_mime.c>
	# Serve .webp with correct MIME type
	AddType image/webp .webp
</IfModule>

<IfModule mod_headers.c>
	# Ensure caches respect image type negotiation
	<FilesMatch "\.webp$">
		Header set Vary "Accept-Encoding"
	</FilesMatch>

	Header unset Vary env=dont-vary
	Header set Vary "Accept" "expr=%{REQUEST_URI} =~ m#\.(webp|jpe?g|png|gif|bmp)$#"
</IfModule>

<IfModule mod_rewrite.c>
	RewriteEngine On

	# Only continue if browser supports WebP
	RewriteCond %{HTTP_ACCEPT} image/webp

	# Skip if request is already .webp
	RewriteCond %{REQUEST_URI} !\.webp$ [NC]

	# 1. Serve .Xwebp.webp if exists (e.g., for quality variants)
	RewriteCond %{REQUEST_FILENAME}.Xwebp.webp -f
	RewriteRule ^(.+)$ $1.Xwebp.webp [T=image/webp,E=no-gzip:1,E=no-brotli:1,E=dont-vary:1,L]

	# 2. Serve plain .webp if exists
	RewriteCond %{REQUEST_FILENAME}.webp -f
	RewriteRule ^(.+)$ $1.webp [T=image/webp,E=no-gzip:1,E=no-brotli:1,E=dont-vary:1,L]

	# 3. Fallback to dynamic conversion via local img2webp
	RewriteRule ^(.+\.(jpe?g|png|gif|bmp))$ atec-webp/img2webp.php?src=$1 [QSA,E=no-gzip:1,E=no-brotli:1,E=dont-vary:1,L]

</IfModule>

#### END atec-webp
