Redirect catch-all for non-matched locations

Hi,

I’m using Caddy mainly as a reverse proxy for a bunch of services like so:

proxy /svc1 http://svc1:8080/ {
  transparent
}
proxy /svc2 http://svc2:8080/ {
  transparent
}
proxy /svc3 http://svc3:8080/ {
  transparent
}

I’d like to redirect all the URLs that don’t match the above 3 locations to another URL - I’ve tried the catch-all redirect: redir http://google.com 301 but then Caddy ignores the 3 proxied services above. Is there a way to achieve this?

FWIW I migrated from Apache and I used RedirectMatch ^ http://google.com there to do the same thing.

Haven’t checked, what the best solution is, but this should work:

rewrite {
  if_op and
  if {path} not_has /svc1
  if {path} not_has /svc2
  if {path} not_has /svc3
  to /x-catch-all
}
redirect /x-catch-all/ https://google.com 301

@stp if you omit if_op, it defaults to and. Also, your solution does not check the start of the request path.

This should work.

rewrite {
  if {path} not_match ^\/(svc1|svc2|svc3)
  to /x-catch-all
}

redirect /x-catch-all/ https://google.com 301
1 Like

@abiosoft thanks for clarifying. I wanted to make it explicit with if_op, but might be better to just use the default without specifying it again.

Wouldn’t starts_with be faster? It does not use a regexp.

rewrite {
  if {path} starts_with /svc1
  if {path} starts_with /svc2
  if {path} starts_with /svc3
  to /x-catch-all
}

redirect /x-catch-all/ https://google.com 301

There is no way to negate starts_with yet. What we want to do is the opposite of this, to check if the request path does not start with those.

1 Like

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