Skip to content

Instantly share code, notes, and snippets.

@mustafauysal
Forked from seeekr/apache-webp-rewrite.conf
Created August 11, 2020 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafauysal/b911e3a50de897009d2d4ee332211b5f to your computer and use it in GitHub Desktop.
Save mustafauysal/b911e3a50de897009d2d4ee332211b5f to your computer and use it in GitHub Desktop.
enabling apache to serve WebP image files if accepted by browser (and .webp files available)
## !! 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment