<IfModule mod_rewrite.c>
  RewriteEngine On

  # Check if browser supports WebP
  RewriteCond %{HTTP_ACCEPT} image/webp
  # Check if .webp version of the image exists
  RewriteCond %{REQUEST_FILENAME}\.webp -f
  # Redirect to the .webp file
  RewriteRule ^(.*)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
</IfModule>

<IfModule mod_headers.c>
  # Tell browser this image is based on its Accept header
  Header append Vary Accept env=REDIRECT_accept
</IfModule>

# Enable WebP MIME type (if server doesn't already)
AddType image/webp .webp
