Match empty user agent

1. The problem I’m having:

How can we match an empty user agent so we can redirect them?

2. Error messages and/or full log output:

none

3. Caddy version:

v2.8.4

4. How I installed and ran Caddy:

Docker, x64, Ubuntu 22.04

a. System environment:

Docker, x64, Ubuntu 22.04

b. Command:

Docker, x64, Ubuntu 22.04

d. My complete Caddy config:

found a V1 tip, unable to convert it to V2

rewrite {
  if {>User-Agent} is ""
  to /no-user-agent-forbidden
}
status 403 /no-user-agent-forbidden

5. Links to relevant resources:

You can match empty User-Agent with a header matcher:

@no_ua header !User-Agent

Then, you can do whatever you want with it:

  • drop it without a response:
abort @no_ua
  • reject with 403:
handle @no_ua { 
  respond 403 
}
  • reject with a custom 403 message:
handle @no_ua { 
  respond "Access denied" 403 
}
  • redirect to a different URL
redir @no_ua https://example.com

etc

3 Likes

Awesome, thanks lot

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