Can anyone translate this rewrite from Apache config to Caddyfile

Can anyone translate one rewrite from Apache .htaccess to Caddyfile, it goes like this:

  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.jpg.webp -f
  RewriteRule (.+)\.jpg$ /wp-content/uploads-webpc/$1.jpg.webp [NC,T=image/webp,E=cache-control:private,L]

Thank you soo much in advance

I actually need 3 but there pretty much the same, but if they could be combined in one that would be great, thanks again

  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.jpg.webp -f
  RewriteRule (.+)\.jpg$ /wp-content/uploads-webpc/$1.jpg.webp [NC,T=image/webp,E=cache-control:private,L]
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.jpeg.webp -f
  RewriteRule (.+)\.jpeg$ /wp-content/uploads-webpc/$1.jpeg.webp [NC,T=image/webp,E=cache-control:private,L]
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{DOCUMENT_ROOT}/wp-content/uploads-webpc/$1.png.webp -f
  RewriteRule (.+)\.png$ /wp-content/uploads-webpc/$1.png.webp [NC,T=image/webp,E=cache-control:private,L]

This is how it’s done in Nginx:

server {
  # ...

  location ~ /wp-content/uploads/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
    if ($http_accept !~* "image/webp") {
      break;
    }
    add_header Vary Accept;
    expires 365d;
    try_files /wp-content/uploads-webpc/$path.$ext.webp $uri =404;
  }
}

But I can’t work it out in Caddy

This part is checking whether the client’s Accept header contains image/webp.

This part checks whether a file ending in .jpeg has an existing .jpeg.webp present equivalent on disk.

This part then proceeds to rewrite to that file, which will ensure the webp variant is served.

In Caddy 2, you’d use a named matcher to sum up all those conditions - including checking for the .webp sidecar - and rewrite to whatever the file matcher finds. The following snippet should handle all three:

@webp {
  header Accept *image/webp*     # client accepts webp
  path *.jpg *.jpeg *.png        # client requested jpg/jpeg/png
  file {
    try_files {path}.webp {path} # look for webp, else fall back
  }
}
rewrite @webp {http.matchers.file.relative}
4 Likes

Thank you so much for that, but is it possible to rewrite this url “/wp-content/uploads/2015/04/image1.jpg” to “/wp-content/uploads-webpc/2015/04/image1.jpg.webp” like in that Nginx configuration in my #3 anwser

Ahh, I missed that path translation.

A shame, we’ll have to reintroduce regex checking. Oh well!

Give something like this a shot, let me know if it works out.

@webp {
  header Accept *image/webp*       # client accepts webp
  path          *.jpg *.jpeg *.png # client requested jpg/jpeg/png
  path_regexp path ^/wp-content/uploads/(.+)$
  file {                           # look for webp, else fall back
    try_files /wp-content/uploads-webpc/{http.regexp.path.1}.webp {path}
  }
}
rewrite @webp {http.matchers.file.relative}
2 Likes

Thank you so much, that’s great, I’m marking your reply as solution, and just one small detail, when you wrote in your reply

Does that mean regex is slower than the other solution(without regex) or it’s just something you wrote?

Regexes are little computers with somewhat complex state machines, so yes if we’re talking number of CPU instructions, they are always slower than specialized algorithms – but 99.9% of users won’t notice any impact, because computers are fast and networks are not.

This topic was automatically closed after 30 days. New replies are no longer allowed.