wp Serve Images in Next-Gen Formats code example

Example: Serve images in nextgen formats wordpress

<IfModule mod_setenvif.c>
  # Vary: Accept for all the requests to jpeg and png
  SetEnvIf Request_URI "\.(jpe?g|png)$" REQUEST_image
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Check if browser supports WebP images
  RewriteCond%{HTTP_ACCEPT}image/webp

  # Check if WebP replacement image exists
  RewriteCond%{DOCUMENT_ROOT}/$1.webp-f

  # Serve WebP image instead
  RewriteRule(.+)\.(jpe?g|png)$$1.webp[T=image/webp]
</IfModule>

<IfModule mod_headers.c>
  Header append Vary Accept env=REQUEST_image
</IfModule>

<IfModule mod_mime.c>
  AddType image/webp .webp
</IfModule>