问题
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