0

I am trying to show webp pictures on my website where there is a jpeg or png picture using LiteSpeed Web Server and an htaccess file.

The snippet is not working because of the name of my webp pictures. Instead of changing the extension, my converter added the new extension to the full name of the picture. Example:

URL of normal pics:

http://domain.tld/path/to/pic/image.png (or jpe?g)

URL of webp pics:

http://domain.tld/path/to/pic/image.png.webp

This is how my htaccess looks:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_URI}  (.*)(\.(jpe?g|png))$
RewriteRule .* %1\.webp [L,T=image/webp] 
</IfModule>
<IfModule mod_headers.c>
Header append Vary Accept env=REDIRECT_accept
</IfModule>
AddType image/webp .webp

I think the problem is in the RewriteRule. The result I am getting is:

http://domain.tld/path/to/pic/image.webp

Instead of:

http://domain.tld/path/to/pic/image.png.webp (or jpe?g.webp)

I tried changing the RewriteRule line to this but it didn't work:

RewriteRule .* %1\.(jpe?g|png).webp [L,T=image/webp]

Can somebody shed some light into this? I am a beginner on htaccess and I know I am not using the correct syntax on this to make it work. This is the website in case you guys want to inspect the page and check the images: LEXSTYLE Any help would be really appreciated. Thanks.

alex_ls
  • 11
  • 1
  • 3
  • How about change "RewriteCond %{REQUEST_URI} (.*)(\.(jpe?g|png))$ RewriteRule .* %1\.webp [L,T=image/webp] " to "RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]"? Looks similar lol not sure if it can help. – Eric May 30 '18 at 13:38
  • Thanks for your response, Eric. I tried that before and it didn't work. I ended up changing the name of the pictures...! lol. Thanks anyway! – alex_ls Jun 07 '18 at 00:07
  • no problem. It just works by change the image name!! COol – Eric Jun 08 '18 at 01:42

1 Answers1

-1

You have an "or" in the rule's output part ((jpeg|jpg)|png), without defining the condition.

To solve that you can use the (.*) to set $1, like this:

RewriteRule ^/path/to/imgdir(.*) /path/to/imgdir$1.webm

Wich is safe, as previously you already had the limitation:

RewriteCond %{REQUEST_URI}  (.*)(\.(jpe?g|png))$

Further you may want to consider to have a double-check for existance of the .webp file in your conditions:

RewriteCond %{DOCUMENT_ROOT}/$1\.webp -f

Also mind that the preferred method became:

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

(which is still useless for use within for for example background files in CSS stylesheets)

Leo
  • 2,331
  • 2
  • 19
  • 17