Skip to content

Instantly share code, notes, and snippets.

@JasperE84
Created July 1, 2022 11:59
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 JasperE84/0e864f947267456d0a460ee76ba5afa7 to your computer and use it in GitHub Desktop.
Save JasperE84/0e864f947267456d0a460ee76ba5afa7 to your computer and use it in GitHub Desktop.
.htaccess webp redirection if exists and client accept header permits
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# check if browser accepts webp
RewriteCond %{HTTP_ACCEPT} image/webp
# check if file is jpg or png
RewriteCond %{REQUEST_FILENAME} (.*\.(jpe?g|png))$
# check if corresponding webp file exists image.png -> image.webp
RewriteCond %1\.webp -f
# serve up webp instead
RewriteRule (.+\.(jpe?g|png))$ $1.webp [T=image/webp,E=accept:1]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment