Need some help with multiple try_files directives (or alternative!)

Hi all, I’ve just transferred my Caddy server (almost 20 sites) to v2 with almost no trouble, thank you so much for this wonderful server!

I’ve got one hiccup, which is mostly I think because I haven’t quite got my head around the new rewrite directive.

What I’m trying to achieve:

  • All requests to* go to index.php?{query} (which works great!)
  • Except requests to* (which should do the same, but to the subdirectory /blog).

Here’s my attempt: {
  root * /var/www/
  php_fastcgi unix//run/php/php7.2-fpm.sock
  try_files {path} {path}/ /index.php?{query}
  try_files /blog/{path} {path}/ /blog/index.php?{query}
  encode gzip

Needless to say, this doesn’t work - the second try_files seems to overwrite the first, so all requests get channeled to /blog.

What am I missing?

Does it work if you remove both of those try_files lines? The php_fastcgi directive does its own try_files logic that should ideally do the trick. If not, I have another suggestion:

handle /blog* {
    try_files {path} {path}/ /blog/index.php
1 Like

Hi there, I’m so sorry I didn’t reply! handle combined with try_files worked perfectly.

1 Like

Thanks for following up!

For future reference, the reason the handle is needed here is because the try_files directive does not accept a matcher argument because it itself is a shortcut for a file matcher with a rewrite.

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