Can someone help me with .htaccess file

thambyz

Active member
Mar 18, 2021
103
41
28
Hi,
I have a folder /assets/admin/images/products which contains .jpg and .png files and another folder /assets/admin/images/products/webp with the webp version of .jpg and .png files with name filename.webp (jpg and png is not added in file names)

How do i serve webp version in supported browsers using htaccess

I found some information at https://github.com/vincentorback/WebP-images-with-htaccess but it only serves from same folder.

Thanks
 

thambyz

Active member
Mar 18, 2021
103
41
28
Hi I can help you with this
Thank you. This code is working if webp image is in same folder. I need to modify it to look in another folder.

Code:
  RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
  RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]
 

tradesman

Well-known member
Mar 9, 2020
367
225
63
Cold North
Thank you. This code is working if webp image is in same folder. I need to modify it to look in another folder.

Code:
  RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
  RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]
what's the dir or name of the folder?
 
  • Like
Reactions: thambyz

thambyz

Active member
Mar 18, 2021
103
41
28
what's the dir or name of the folder?
This is the folder with jpg,jpeg and png images (ex: image.png)
/assets/admin/images/products


This is the folder with webp version(eg: image.webp)
/assets/admin/images/products/webp
 

tradesman

Well-known member
Mar 9, 2020
367
225
63
Cold North
The solution is already in the GitHub link that you posted.

replace the "path to image" with the actual path to the file/folder of your resources.


1667692650423.png

You can check these info too:

 
  • Like
Reactions: thambyz

thambyz

Active member
Mar 18, 2021
103
41
28
The solution is already in the GitHub link that you posted.

replace the "path to image" with the actual path to the file/folder of your resources.

Thanks for the update. But the issue is it is not possible to edit the actual code. The code is in PHP with codeigniter framework which i don't have idea and i don't want to touch the actual code, that is the reason i am looking for htaccess option. And this is also the reason i don't want to put optimized webp images in actual folder so i can easily revert the changes done. But i think this is the easiest way to put webp images inside images folder and later delete webp images later if required.
 

thambyz

Active member
Mar 18, 2021
103
41
28
Below code is working.


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}/assets/admin/images/products/webp/$1.webp -f

  # Serve WebP image instead
  RewriteRule (.+)\.(jpe?g|png|gif)$ webp/$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>
 

Forum statistics

Threads
69,206
Messages
908,351
Members
236,895
Latest member
jeremcastdlp

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu