htaccess config to serve jxl / avif if file exist

106 views Asked by At

I want to configure the htaccess file on a wordpress installation / apache server to serve a JpegXL if the browser supports it and if not serve a .avif file. If both formats are not supported serve the original JPEG. The files are names image_name.jpg image_name.avif image_name.jxl and are accessible on the server.

<IfModule mod_rewrite.c>
RewriteEngine On
# Serve JXL if the browser supports it and the file exists
RewriteCond %{HTTP_ACCEPT} image/jxl
RewriteCond %{DOCUMENT_ROOT}/$1.jxl -f
RewriteRule (.+)\.(jpg|jpeg)$ $1.jxl [T=image/jxl,E=accept:true]

#Serve AVIF if the browser supports it and the file exists, and JXL wasn't served
RewriteCond %{ENV:REDIRECT_accept} !=true
RewriteCond %{HTTP_ACCEPT} image/avif
RewriteCond %{DOCUMENT_ROOT}/$1.avif -f
RewriteRule (.+)\.(jpg|jpeg)$ $1.avif [T=image/avif]
</IfModule>

Currently the condition RewriteCond %{DOCUMENT_ROOT}/$1.jxl -f and RewriteCond %{DOCUMENT_ROOT}/$1.avif -f evaluate to false and the file is not replaced. When deleting this line the correct file is displayed

0

There are 0 answers