Routes with / or %2F

1. The problem I’m having:

I want to operate on a route called routine/s//. This is not possible, so its coded as routine/s%2F%2F.
There is a map directive in my Caddyfile that does something like this mapping

map {path} {npath} {
  "/routine/s%2F%2F" "/hashed/1f2f3f4f5"
}

Actually the line inside the map block is imported from a file called prettyurls.

However, it seems that Caddy is translating %2F to / before getting to the map directive.

So I changed the map (actually added a line to prettyurls) to

map {path} {npath} {
  "/routine/s%2F%2F" "/hashed/1f2f3f4f5"
  "/routine/s//" "/hashed/1f2f3f4f5"
}

This does not work either. / seems to be the only character with a problem. ? coded as %3F is dealt with as expected.

I was wondering whether I should be looking at another placeholder (not {path}) to get the request without the %2F/ mapping.

2. Error messages and/or full log output:

3. Caddy version:

v4.6.4

4. How I installed and ran Caddy:

a. System environment:

docker

b. Command:

c. Service/unit/compose file:

d. My complete Caddy config:

	root * /usr/share/caddy

  map {path} {npath} {
    import /usr/share/caddy/assets/prettyurls
  }

  route {
    error /hashed* 403
    try_files {npath}.html {path}.html {path}
  }

  file_server

  handle_errors {
		@404 {
			expression {http.error.status_code} == 404
		}
		@403 {
			expression {http.error.status_code} == 403
		}
		rewrite @404 /404.html
		rewrite @403 /403.html
		file_server
  }

5. Links to relevant resources:

I spent some time on this for v2.6:

The release notes also have details.

These changes affect the path matcher and the rewrite directive, so I don’t know if it got into map.

Can you temporarily try changing your config to use the path matcher and rewrite directives (try_files uses a file matcher and a rewrite directive under the hood) to see if you get the desired behavior, after reading about it in the linked notes?

After more testing trying to reconfigure as requested, I have discovered that the Caddy map is working as expected.
The directive

map {path} {npath} {
  import prettyurls
}

where prettyurls contains lines like

"/routine/%2F" "/hashed/1a2a3a"

does work as described deep in the Caddy Documentation.

The explanation in the notes you linked to were excellent and the solution you have adopted is - I think - a good one.

Because of your extensive notes, I also discovered another URL-rewrite unrelated to Caddy in my test setup. So in fact the actual problem I was encountering was not related to Caddy.

Thank you for the help.

2 Likes

Wow, that’s awesome!

I’m currently working on a new website and new docs, I will be sure to explain this better in the new documentation (as it’s a relatively new feature).

Thanks for following up :slight_smile:

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