Help with URI strip_prefix / redirect

1. Caddy version (caddy version):

2.3.0

2. How I run Caddy:

Caddyfile is used

a. System environment:

OS X
AWS Linux 2 AMI

Not using Docker

b. Command:

caddy run

c. Service/unit/compose file:

n/a

d. My complete Caddyfile or JSON config:


appsite.local localhost {	
    reverse_proxy 127.0.0.1:8000
    file_server
}

applink.local {

    redir https://appsite.local/app?token={uri} permanent

}

3. The problem I’m having:

I am trying to redirect clients coming in to one domain (similar to a URL shortener) to a different domain.

the pattern coming in is

applink.local/<token here>

the pattern coming out (redirect) I want is:

appsite.local/app?token=<token here>

at which I am failing miserably.

4. Error messages and/or full log output:

5. What I already tried:

I have tried

uri strip_prefix /

but this doesn’t seem to remove the leading /

6. Links to relevant resources:

You accidentally opened a thread under the Wiki category. I’ve moved it to the Help category.

Please fill out the help thread template (you can find it by clicking New Topic and copying it into here)

1 Like

Ah, I see. Thanks for filling out the template!

So since you want to only grab a segment of the path, you’ll want to use a path_regexp matcher to only capture the part of the path you care about (i.e. everything after /).

applink.local {
	@token path_regexp token ^/(.*)$
	redir @token https://appsite.local/app?token={re.token.1} permanent
}
1 Like

thanks, that’s perfect.

1 Like

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