How to try_files and rewrite part of the path?

And again I’m trying to find the best way for simple rewriting path, but can’t understand a synthax of the Caddyfile.

With nginx I used this type construction: try_files /first/$1 /second/$1 =404;

In Caddy I try this:

try_files {path} {
  path_regexp first ^/first/(.*)
  rewrite /second/{re.first.1}
}

But this doesn’t work.
An in my case I have a troubled circumstances — this block located inside handle_path.

Could you advise where I can look for answer? :slight_smile:

I think you’re just looking to do this:

try_files /first/{path} /second/{path}

I don’t know why, but in my case it doesn’t work:

host {
  handle_path /first/* {
    root * /first
    try_files /first/{path} /{path}
  }
  
  root * /all
  file_server
}

At the end I’ve solved it with change the root folder. It’s not the answer on my question, but it works for my case. Hope someone find it useful

host {
  handle_path /first/* {
    root * /first
    
    @try_files {
      not {
        file {
          try_files {path}
        }
      }
    }
    handle @try_files {
      root * /all
    }
  }
}

You can write it more simply like this:

example.com {
	handle_path /first/* {
		root * /first
		@try_files not file {path}
		handle @try_files {
			root * /all
		}
	}
}
2 Likes

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