How to serve webp images using htaccess?

…衆ロ難τιáo~ 提交于 2020-07-16 06:48:23

问题


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" 

If the webp file exists and if not I need:

"myurl.com/foo.jpg"

as a fallback solution.


回答1:


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.



来源:https://stackoverflow.com/questions/56100014/how-to-serve-webp-images-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!