D
D
Domobisnes2018-02-02 13:40:18
htaccess
Domobisnes, 2018-02-02 13:40:18

How to autocorrect image extensions using htaccess?

Friends, help me understand why autocorrection of extensions via htaccess does not work. Essentially, there should be a replacement for jpg/png with webp if the user's browser supports webp. The post itself always contains a .jpg picture
. The replacement works if you only delete the picture with the .jpg extension in the site folder or change it to .jpeg (using the poke method).
And I know that the console will still show that the picture has the .jpg extension and the fact that that there is a substitution for webp is visible only in the type column of the web developer console.
Here is the actual code:

<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)$ $1.webp [T=image/webp,E=accept:1]
</IfModule>

<IfModule mod_headers.c>
  Header append Vary Accept env=REDIRECT_accept
</IfModule>

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitalii Bryl, 2020-06-17
@shuffler_agr0s

Using the webp format for images. How to implement this with .htaccess?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question