Help with Apache Reqrite to Caddy (WP WebP Express)

The .htaccess file has

 RewriteCond %{HTTP_ACCEPT} image/webp
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{DOCUMENT_ROOT}/wp-content/webp-express/webp-images/doc-root/wp-content/$1.$2.webp -f
 RewriteRule ^\/?(.*)\.(jpe?g|png)$ /wp-content/webp-express/webp-images/doc-root/wp-content/$1.$2.webp [NC,T=image/webp,QSD,E=EXISTING:1,L]

and it also has

 RewriteCond %{HTTP_ACCEPT} image/webp
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{QUERY_STRING} (.*)
 RewriteRule ^(.*)\.(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?wp-content=wp-content&%1 [NC,L]

I am trying to convert these to Caddy rewrite rules with no success, I have tried using the regex rules for rewrite on the last rewrite block, but that didn’t work, I also then tried the extension rules which also did not work. Any help would be greatly appreciated.

It looks like Apache is rewriting

  1. If the requested file is a jpeg/png file
  2. If the file actually exists on disk
  3. If the client accepts webp

The main problem is with 2. Caddy’s rewrite can’t conditionally check whether a file exists before operating.

The closest approximation to this functionality is to rewrite to multiple targets, which will have Caddy pick the first one in the list that exists on disk.

There might be some workaround, but I can’t think of one right now. My advice, generally speaking in regards to WordPress (given how unbelievably common it is for WP developers to rely on quirks of Apache, since it’s deployed 99.99% of the time in shared cPanel hosting) is to just have Caddy proxy to Apache.

Can the second code example be converted though? It doesn’t seem to check for files existing, the first code block is not required for the plugin to work.

Yeah, it does, unfortunately.

RewriteCond %{REQUEST_FILENAME} -f

This line effectively says, “if the requested path is an existing file”. It’s pretty rare I see the -f flag, far more common I see the opposite, !-f (which is “if the requested path is not an existing file”), and also commonly paired with !-d (“the requested path is not an existing directory”), both of which Caddy can handle using the fallback method.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.