1. The problem I’m having:
I am looking for an efficient way to serve webp variants of jpgs and pngs to supported clients via Caddy. The webps have the same name as the original formats (https://example.com/image.webp
and https://example.com/image.png
).
The below directives seem to work fine for Apache and LiteSpeed, though I have no idea if they are efficient/optimized. A Caddyfile equivalent would be great if possible.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
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>
AddType image/webp .webp
2. Error messages and/or full log output:
There is no error message. I asked three AIs to convert the code, and the outputs provided by all three are either invalid or don’t seem to work. From what I understand, the code they provided is looking for image.jpg.webp
(or image.png.webp
) instead of image.webp
.
Response by Claude AI:
@images {
path *.jpg *.jpeg *.png
}
handle @images {
try_files {path}.webp {path}
header Vary Accept
header {path}.webp Content-Type image/webp
}
Another one by Claude AI (invalid syntax, ChatGPT had something similar):
@webp_available {
file {
try_files {path}.webp
}
header_regexp Accept image/webp
}
handle_path /*.jpg /*.jpeg /*.png {
rewrite @webp_available {path}.webp
header Vary Accept
}
header /*.webp Content-Type image/webp
Google Gemini:
http://localhost:80 {
root /path/to/your/website
file_server {
root * {
try_files {
$uri.webp
$uri
}
}
}
rewrite {
if {
path =~ ^(.+)\.(jpe?g|png)$
header Accept contains image/webp
file exists $1.webp
} {
path {
$1.webp
}
header Accept {
image/webp
}
}
}
}
3. Caddy version:
2.8.4
4. How I installed and ran Caddy:
Caddy’s official deb repository
a. System environment:
Debian 12.6 amd64
b. Command:
n/a
c. Service/unit/compose file:
n/a
d. My complete Caddy config:
n/a
5. Links to relevant resources:
n/a