Code Monkey home page Code Monkey logo

webp-images-with-htaccess's Introduction

WebP images with htaccess

This snippet detects if the browser supports 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.

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.

<IfModule mod_rewrite.c>
  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]
</IfModule>

<IfModule mod_headers.c>
  # Vary: Accept for all the requests to jpeg, png and gif
  Header append Vary Accept env=REQUEST_image
</IfModule>

<IfModule mod_mime.c>
  AddType image/webp .webp
</IfModule>

Preferred solution

Controlling your files using htaccess sure is fun, but a more responsible way is to use the <picture>-element instead of this solution. It has great support in all major browsers and has a built-in fallback for those without it.

<picture>
  <source srcset="/path/to/image.webp" type="image/webp">
  <img src="/path/to/image.jpg" alt="">
</picture>

webp-images-with-htaccess's People

Contributors

echu2013 avatar miyanaga-private avatar vincentorback avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webp-images-with-htaccess's Issues

REQUEST_image enviroment variable never set

SetEnvIf Request_URI "\.(jpe?g|png)$" REQUEST_image

I don`t know why but this is not working for me with Apache/2.4.43

This doest work for me:
``SetEnvIf Request_URI ".(jpeg|jpg|png)$" REQUEST_image
Optional "?" is valid regex but my apache does not match anything at all with this.

Thank you for this code which was greatly useful for me!

env=REQUEST_image

What is the point of env=REQUEST_image in Header append?
Can't find it anywhere in apache docs. Is it relevant only to webp-express plugin?

Right click, "save as" selects a .jpg file?

I'm not sure if this is on my end only, but when using Chrome, right click on image, "save as", it tries to save a .jpg version even when a .webp version is displayed? This also happens in for example Firefox when using the Page info (ctrl+i on Windows) and save image button.

How do I make it scan every image?

It seems like as soon as it sees one image.jpg that has an image.webp it assumes every other jpg has a webp counterpart and tries to show everyOther.jpg to everyOther.webp but what if users (from a blog let's say) add a picture.jpg then it tries to show the picture.webp and it breaks.

Cant get it to work

I've tried to do this as a test, but it wont work. I'm using newest Chrome 35 Beta and I've tested a webp image so I'm sure I dont have a wrong setting. Does the script need an update or could it be in my end?

400 Error - Bad Request

Hi, I am using this code with a few small modifications to reflect my file structure. I believe I have got the structure correct because if I change [T=image/webp,E=accept:1] to [R=302] it successfully redirects me to the correct webp image.

For some reason though, when I use [T=image/webp,E=accept:1] I get a 400 error. Can you help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.