How to serve webp images using htaccess?

后端 未结 2 492
野的像风
野的像风 2021-01-24 21:04

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         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 21:47

    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.

提交回复
热议问题