Skip to content

Instantly share code, notes, and snippets.

@gravataLonga
Forked from seeekr/apache-webp-rewrite.conf
Created December 20, 2017 12:46
Show Gist options
  • Save gravataLonga/181e7b2b22855d4c5a1bc2e032a89bc0 to your computer and use it in GitHub Desktop.
Save gravataLonga/181e7b2b22855d4c5a1bc2e032a89bc0 to your computer and use it in GitHub Desktop.
enabling apache to serve WebP image files if accepted by browser (and .webp files available)
# 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment