├── .htaccess ├── README.md ├── images ├── image1.jpg ├── image1.webp └── image2.jpg ├── index.html └── modernizr-webp.js /.htaccess: -------------------------------------------------------------------------------- 1 | 2 | RewriteEngine On 3 | 4 | # Check if browser supports WebP images 5 | RewriteCond %{HTTP_ACCEPT} image/webp 6 | 7 | # Check if WebP replacement image exists 8 | RewriteCond %{DOCUMENT_ROOT}/$1.webp -f 9 | 10 | # Serve WebP image instead 11 | RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image] 12 | 13 | 14 | 15 | # Vary: Accept for all the requests to jpeg, png and gif 16 | Header append Vary Accept env=REQUEST_image 17 | 18 | 19 | 20 | AddType image/webp .webp 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebP images with htaccess 2 | 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). 3 | 4 | ## Usage 5 | Place the following in your .htaccess file and jpg/png/gif images will be replaced with WebP images if found in the same folder. 6 | ```apache 7 | 8 | RewriteEngine On 9 | 10 | # Check if browser supports WebP images 11 | RewriteCond %{HTTP_ACCEPT} image/webp 12 | 13 | # Check if WebP replacement image exists 14 | RewriteCond %{DOCUMENT_ROOT}/$1.webp -f 15 | 16 | # Serve WebP image instead 17 | RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image] 18 | 19 | 20 | 21 | # Vary: Accept for all the requests to jpeg, png and gif 22 | Header append Vary Accept env=REQUEST_image 23 | 24 | 25 | 26 | AddType image/webp .webp 27 | 28 | ``` 29 | 30 | ## Preferred solution 31 | 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. 32 | ```html 33 | 34 | 35 | 36 | 37 | ``` 38 | -------------------------------------------------------------------------------- /images/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vincentorback/WebP-images-with-htaccess/45798fb1d0f4b0f13bb2ba85ed7eda2f3dcde85f/images/image1.jpg -------------------------------------------------------------------------------- /images/image1.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vincentorback/WebP-images-with-htaccess/45798fb1d0f4b0f13bb2ba85ed7eda2f3dcde85f/images/image1.webp -------------------------------------------------------------------------------- /images/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vincentorback/WebP-images-with-htaccess/45798fb1d0f4b0f13bb2ba85ed7eda2f3dcde85f/images/image2.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | WebP image replacement with htaccess 6 | 32 | 33 | 34 |

WebP image replacement with htaccess

35 |

Browser support WebP: loading...

36 | 37 |
38 |
39 |

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

40 | 41 |
42 |
43 |

2. This image is only available as a jpg.

44 | 45 |
46 |
47 |

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

48 | 49 |
50 |
51 | 52 | 53 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /modernizr-webp.js: -------------------------------------------------------------------------------- 1 | /*! modernizr 3.6.0 (Custom Build) | MIT * 2 | * https://modernizr.com/download/?-webp-setclasses !*/ 3 | !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