I would like to serve .webp images if the image (jpg or png) is available as webp. If not I want to show the jpg or png.
RewriteCond %{HTTP_ACCEPT} image/webp
Re
Some browser don't fully support webp, you should add a condition to send only to supported browsers :
<Files *.webp>
Header set Vary "Accept-Encoding"
AddType "image/webp" .webp
AddEncoding webp .webp
</Files>
RewriteCond %{HTTP:Accept} image/webp
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule ^(.*)$ $1.webp [L]
If the requested URL ends in jpeg
or png
, you might test for the corresponding webp
with
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule ^ %{REQUEST_FILENAME}.webp [L]
You may be more restrictive, if you like
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule \.(jpe?g|png)$ %{REQUEST_FILENAME}.webp [L]
If the condition and rule don't match, the request will fall through and serve the file (.jpg/.png) as is.