How to serve webp images using htaccess?

后端 未结 2 427
野的像风
野的像风 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:36

    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]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题