DEV Community

Discussion on: How to use WebP images

Collapse
 
_brendamalone profile image
Brenda Malone • Edited

We throw this into htaccess for serving next gen images. You guys see any harm here? Thanks.

#SERVE NEXT GEN IMAGES

# Vary: Accept for all the requests to jpeg and png
SetEnvIf Request_URI ".(jpe?g|png)$" REQUEST_image


RewriteEngine On
# Check if browser supports WebP images
RewriteCond %{HTTP_ACCEPT} image/webp
# Check if WebP replacement image exists
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
# Serve WebP image instead
RewriteRule (.+).(jpe?g|png)$ $1.webp [T=image/webp]


Header append Vary Accept env=REQUEST_image


AddType image/webp .webp

END OF SERVE NEXT GEN IMAGES

Collapse
 
dailydevtips1 profile image
Chris Bongers

Interesting, haven't seen this in use before.
But won't this just change the extension of the file? It won't actually serve an optimized webP image?

Collapse
 
_brendamalone profile image
Brenda Malone

It shouldn't.
We use Optimus to optimize the images and make webp versions.
The problem is that they weren't being served, but the script takes care of that.

But interesting question, I will investigate.
Thanks.
B