Get subdomain from host

1. My Caddy version (caddy version):

2.0

d. My complete Caddyfile or JSON config:

{
    "apps": {
        "http": {
            "servers": {
                "myserver":{
                    "listen":[":443"],
                    "routes":[{
                        "match":[
                            {
                                "host": ["*.mylodocs-dev.com"]    
                            }
                        ],
                        "handle": [
                        {
                            "handler":"rewrite",
                            "uri": "/subdomain{http.request.uri}"
                        }]
                    }]
                }
            }
        }
    }
}

3. The problem I’m having:

I’m trying to extract the subdomain from the host in my json config. and use that to append to a path.
Is that possible. For example i want to take subdomain.mylodocs-dev.com and then proxy that to someotherurl.com/subdomain. I’m trying to make it so that I can only have one route and it doesn’t matter what the subdomain is.

You can use the {http.request.host.labels.2} placeholder to get your subdomain.

See here:

2 Likes

Thanks! I missed that in the documentation

1 Like

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