如何使用htaccess提供webp图像? [英] How to serve webp images using htaccess?
本文介绍了如何使用htaccess提供webp图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果图像(jpg或png)可用作webp,我想提供.webp图像.如果没有,我想显示jpg或png.
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
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.(jpe?g|png)(.*)$ $1.$2.webp [NC,L]
AddType image/webp .webp
这个问题,我需要最终的网址是这样的:
The problem I need the final URL to be like:
"myurl.com/foo.jpg.webp"
如果webp文件存在,并且我不需要:
If the webp file exists and if not I need:
"myurl.com/foo.jpg"
作为后备解决方案.
推荐答案
如果请求的URL以jpeg
或png
结尾,则可以使用
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]
如果条件和规则不匹配,请求将失败并按原样提供文件(.jpg/.png).
If the condition and rule don't match, the request will fall through and serve the file (.jpg/.png) as is.
这篇关于如何使用htaccess提供webp图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文