How to rewrite query removing certain fields

I read rewrite (Caddyfile directive) — Caddy Documentation section for a while, still feel confused,

1. The problem I’m having:

in my case I want to reverse proxy requests, remove prefix in path, and remove certain fields from query.

Say original request is:

POST /a/b/c?d=e&f=g

the request after rewriting is expected to be:

POST /b/c?f=g

if there are more query params, they are passed through. what is the correct configure for this.

I use caddy 2.8 on both linux and macos.


Please completely fill out the help topic template. We need the context of seeing your existing config and logs to be able to help effectively.

Regarding URI prefix, you probably want uri strip_prefix, but handle_path may serve you better – but we’d need to see your config to say which is best.

Regarding query rewrites, we’re a bit behind on our docs updates, but we now have a uri query operations (see rewrite: Implement `uri query` operations by armadi1809 · Pull Request #6120 · caddyserver/caddy · GitHub) which can be used to drop specific query args.


thanks, rewrite and strip_prefix is what I need.

based on your message I got my config, which works on proxy and path rewrite: {
  # rewrite * /api{uri}
  uri strip_prefix /a
  uri query -d
  reverse_proxy {
    header_up Host {upstream_hostport}
  tls internal

this is almost whole config I want in my case(without my own domains and paths).

however uri query -d does not seem to work(according to rewrite: Implement `uri query` operations by armadi1809 · Pull Request #6120 · caddyserver/caddy · GitHub),

caddy -v
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

and looking forward to upcoming docs on uri query.

Works for me :man_shrugging:

:8881 {
	uri query -d
	respond {query}
$ curl "http://localhost:8881/foo?a=a&b=b&d=d"

you are right. it’s working as I split out config from my demo and retried. (could not reproduce problem after some messy edits and restarts)

1 Like