Home > database >  How to serve webp from a different folder using .htaccess
How to serve webp from a different folder using .htaccess

Time:11-07

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 of image from different folder using htaccess

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

I tried below htaccess code, but it didn't work

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

CodePudding user response:

Your code is good but you don't need %{DOCUMENT_ROOT} on the last line with RewriteRule. This is the problem.

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