Clarity on the map directive example

This is a general query on the map (Caddyfile directive) — Caddy Documentation, specifically the example. There are a couple of entries in the first column of the table that read ~.*\.net$ and ~.*\.xyz$. The supporting explanation states: ‘…if the request is for any host that ends in .net or .xyz…’

I’m trying to figure out how these entries were created as I’d like to include an entry for a map table that uses the placeholder {path} as the source with the supporting explanation being ‘…if the request is for any path that starts with /xyz…’ Is this possible?

1 Like

If the condition starts with ~ then it’s a regexp. Is that what you missed? If not, I’m not sure I understand the question.

2 Likes

I had no idea! Thanks for the heads up.

1 Like

It’s mentioned in the syntax doc:

<input> is the input value to match. If prefixed with ~, is treated as a regular expression.

1 Like

Also see this PR, capture groups will be usable in the next release (or build from the latest on master)

Seriously, regex pattern matching is new to me. Can you please advise whether the regex expression ~^/xyz.* meets the stated requirement?

https://regex101.com/ is your friend :slight_smile:

2 Likes

Yep, it should. (And like @matt said, use regex101.com, it’s really good to try things out and explain in the sidebar what each character does).

1 Like

I believe I’ve used regular expressions in sed and grep, but I generally adapted canned responses I’d find online without any real understanding. I feel more confident now that I’ll be able to approach those tools with less trepidation. Thanks for enlightening me, gentlemen. :smile:

1 Like

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