## !! This snippet has been updated, but not tested in practice. It should work, please check / leave comments in case it doesn't. !! ## | |
# originally from https://groups.google.com/a/webmproject.org/group/webp-discuss/browse_thread/thread/196ac4ea705688d8 | |
<IfModule mod_rewrite.c> | |
# TODO: don't forget to put | |
# AddType image/webp .webp | |
# in your mods-available/mime.conf | |
# (though this is optional because we're setting | |
# the mime type manually in the RewriteRule) | |
# Enable rewrite | |
RewriteEngine On | |
# Does browser support WebP? | |
RewriteCond %{HTTP_ACCEPT} \bimage/webp\b | |
# Capture image name | |
RewriteCond %{REQUEST_URI} (.*)\.(jpe?g|png)$ | |
# if you don't have all jpg/png images available | |
# as webp then you want to uncomment the next line | |
# so apache first checks if there is a webp file | |
# otherwise leave it disabled as it removes the | |
# need to query the disk | |
#RewriteCond %{DOCUMENT_ROOT}%1.webp -f | |
# Route to WebP image | |
RewriteRule .* %1\.webp [L,T=image/webp] | |
<IfModule mod_headers.c> | |
<FilesMatch "\.(jpe?g|png)$"> | |
Header append Vary Accept | |
</FilesMatch> | |
</IfModule> | |
</IfModule> |
This comment has been minimized.
This comment has been minimized.
For some reason my Apache added an extra dot, so I had to remove "." from the RewriteRule to make it work. (If someone is having problems getting it to work.) |
This comment has been minimized.
This comment has been minimized.
You should add some rules to add "Vary Accept" if Apache served the webp format, or the image could be cached on proxy or CDN in an incorrect format. |
This comment has been minimized.
This comment has been minimized.
is wrong as @tuhoojabotti noticed - pngs wont work like that. should be:
|
This comment has been minimized.
This comment has been minimized.
Perhaps this is what you need...
|
This comment has been minimized.
This comment has been minimized.
Thanks everyone for the comments. |
This comment has been minimized.
This comment has been minimized.
I've incorporated the feedback, though I haven't actually tested it. Should still be a good enough starting point. Thanks all! |
This comment has been minimized.
This comment has been minimized.
Hi, sorry I didn't put any credits... I was looking for this to enable webp on a single plain html page and landed here... I end up writing it... but tired of re-inventing the weal :-/ This is inspired by rules from wordpress plugin https://github.com/rosell-dk/webp-express (the guy has done an excellent job) ADDVARY variable has a meaning and without it apache won't add the Vary header on a rewrite rule. The other, I'm not sure... I know it works with it, but could have a meaning for apache as is could be for some other rules hook... in doubt I prefered keeping it. I could not find documentation on ADDVARY magic variable, but I did test without it and Vary header goes missing too. This is based on heavily tested code... Wether one variable is needed over who knows what specific context, getting to that line took the dev of webp-express many many testing over many context... took him some effort. It ads the Vary header so it respects http standards... Note: in case CDN ignored the Vary, one could add the 'Cache-Control: Private' which explicitly bans reverse proxy caching and still take advantage of browser cache. Edit: |
This comment has been minimized.
thinking the redirect should be a permanent one... would the browser then not send another request to the original jpg/png image when it encounters that url once again and ask for the webp version immediately / get that one from cache immediately? will have to check that.
for pages with lots of images this redirect version would not be acceptable if that were not the case. if caching works regardless, then this would be fine.