Search code examples
caddycaddyfile

Do not redirect specific user agents with Caddy


Considering this Caddy Caddyfile config

http://www.example.com {
  redir https://{host}{uri}
}

How can I make the redirect not happen for user agents that contain the words "foo", "bar", "b a z" or another arbitrary string?

I tried a named matcher with header_regexp but without negative lookahead I don't know how to write the regex. Maybe there is another way?

I tried a named matcher using an expression like header_regexp('User-Agent', '(foo)') || header_regexp('User-Agent', '(bar)') || header_regexp('User-Agent', '(another one)') but since I can not negate the matcher with not, I cannot use it. Maybe one could write the expression itself in a negated way?


Solution

  • This works:

    http://www.example.com {
      @notadumbclient `
        header_regexp('User-Agent', 'foo') == false
        && header_regexp('User-Agent', 'bar') == false
        && header_regexp('User-Agent', 'Something Else') == false
      `
      redir @notadumbclient https://{host}{uri} permanent
    }