Repository: vincentorback/WebP-images-with-htaccess Branch: master Commit: 45798fb1d0f4 Files: 4 Total size: 6.6 KB Directory structure: gitextract_7hu7rzmg/ ├── .htaccess ├── README.md ├── index.html └── modernizr-webp.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .htaccess ================================================ 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|gif)$ $1.webp [T=image/webp,E=REQUEST_image] # Vary: Accept for all the requests to jpeg, png and gif Header append Vary Accept env=REQUEST_image AddType image/webp .webp ================================================ FILE: README.md ================================================ # WebP images with htaccess This snippet detects if the browser [supports WebP](http://caniuse.com/#search=webp) images and then serves a .webp image instead of jpg/png if a .webp file is available at the same location as the supplied jpg/png/gif. Read more about the webp format and other ways to serve it here: [https://images.guide](https://images.guide/#how-do-i-serve-webp). ## Usage Place the following in your .htaccess file and jpg/png/gif images will be replaced with WebP images if found in the same folder. ```apache 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|gif)$ $1.webp [T=image/webp,E=REQUEST_image] # Vary: Accept for all the requests to jpeg, png and gif Header append Vary Accept env=REQUEST_image AddType image/webp .webp ``` ## Preferred solution Controlling your files using htaccess sure is fun, but a more responsible way is to use the ``-element instead of this solution. It has [great support](https://caniuse.com/webp) in all major browsers and has a built-in fallback for those without it. ```html ``` ================================================ FILE: index.html ================================================ WebP image replacement with htaccess

WebP image replacement with htaccess

Browser support WebP: loading...

1. This image is available as both webp and jpg.

2. This image is only available as a jpg.

3. This div has an image background set with a jpg image. It should serve a webp image if supported.

================================================ FILE: modernizr-webp.js ================================================ /*! modernizr 3.6.0 (Custom Build) | MIT * * https://modernizr.com/download/?-webp-setclasses !*/ !function(e,n,A){function o(e,n){return typeof e===n}function t(){var e,n,A,t,a,i,l;for(var f in r)if(r.hasOwnProperty(f)){if(e=[],n=r[f],n.name&&(e.push(n.name.toLowerCase()),n.options&&n.options.aliases&&n.options.aliases.length))for(A=0;A