Redirect * domains

1. Caddy version (caddy version):

v2.4.0 h1:yHnnbawH2G3ZBP2mAJF4XBLnJanqhULLP/wu01Qi9Io=

2. How I run Caddy:

systemd script provided by caddy

a. System environment:

ubuntu 20.14 / systemd

b. Command:

n/a

c. Service/unit/compose file:

n/a

d. My complete Caddyfile or JSON config:

{
#    debug
    on_demand_tls {
        ask http://127.0.0.1:5000/_domain_check
    }
}

:443 {

    # API RELATED CONFIGS

    # allow password reset
    @api_password_reset {
        header Host api.*
        path /password_reset*
    }
    # TODO - I'd like to redirect api.$domain/$uri to www.$domain/$uri
    handle @api_password_reset {
        # NOTE - this is pointing at app, not api.
        reverse_proxy 127.0.0.1:5000
    }

    # allow activate
    @api_activate {
        header Host api.*
        path /activate*
    }
    handle @api_activate {
        # NOTE - this is pointing at app, not api.
        reverse_proxy 127.0.0.1:5000
    }

    # allow admin
    @api_admin {
        header Host api.*
        path /admin*
    }
    handle @api_admin {
        reverse_proxy 127.0.0.1:5001
    }
    # api static assets
    @api_static {
        header Host api.*
        path /static*
    }
    handle @api_static {
        file_server /static/* {
            root /var/www/cb/api/api/
        }
    }

    # api media assets
    @api_media {
        header Host api.*
        path /media*
    }
    handle @api_static {
        file_server /media/* {
            root /var/www/cb/api/api/
        }
    }

    # APP RELATED CONFIGS
    @app header Host www.*

    # serve static files
    handle @app {
        file_server /static/* {
            root /var/www/cb/app/
        }
    }

    # proxy to uwsgi server and/or redirec to www
    handle @app {
        reverse_proxy 127.0.0.1:5000
    }

    # send non-www to www
    @needs_www {
        not header Host api.*
    }
    handle @needs_www {
        redir https://www.{host}{uri}
    }

    tls josh.anyan@removed.org {
        on_demand
        dns route53 {
            max_retries 10
            aws_profile "default"
        }
    }

    log {
        output file /tmp/caddy.log {
            roll_size 100MiB
            roll_keep 10
            roll_keep_for 336h
        }
    }
}


3. The problem I’m having:

I’d like to redirect api.$domain/$uri to www.$domain/$uri
I might be able to use labels but I’m not sure if counting from the right is going to work. If i have api.some-domain.co.uk and api.some-domain.com then my label logic might not work since I need labels.0-3 in the first instance and labels.0-2 in the second. I almost need slice, or pop, functionality to grab everything except the subdomain.

4. Error messages and/or full log output:

n/a

5. What I already tried:

I’m currently doing this in the app itself (not caddy) and I don’t have a great way to testing a .co.uk domain given the auto-tls (can’t just fake a request).

6. Links to relevant resources:

I might be able to use map, but it’s not obvious to me how to do so: map (Caddyfile directive) — Caddy Documentation

You can use a header_regexp matcher to capture the value you need, like header_regexp apihost Host api\.(.*) and then use the captured value with redir https://www.{re.apihost.1}{uri} permanent

2 Likes

Perfect, thanks again!

A trimmed down version for future visitors:

:443 {
    @api_password_reset {
        header_regexp apihost Host api\.(.*)
        path /password_reset*
    }
    handle @api_password_reset {
        redir https://www.{re.apihost.1}{uri} permanent
    }
}
1 Like

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