Redirect non-www to www in json

Hi,
how can i redirect all domains
domain.tls to www.domain.tls using JSON Config

1. Caddy version (v2.2.1):

2. How I run Caddy:

using api http://127.0.0.1:2019/config/

a. System environment:

ubuntu 18

b. Command:

curl --header "Content-Type: application/json" \
  --request PATCH \
  --data '{
    "apps": {
        "http": {
            "servers": {
                "srv0": {
                    "listen": [":443"],
                    "routes": [
                        {"match": [{"host": ["domain1.tls"]}],"handle": [{"handler": "file_server","root": "/path/to/domain1.tls"}]},
                        {"match": [{"host": ["domain2.tls"]}],"handle": [{"handler": "file_server","root": "/path/to/domain2.tls"}]}
                    ]
                }
            }
        }
    }
}' \
  http://localhost:2019/config/

d. My complete Caddyfile or JSON config:

{
    "apps": {
        "http": {
            "servers": {
                "srv0": {
                    "listen": [":443"],
                    "routes": [
                        {"match": [{"host": ["domain1.tls"]}],"handle": [{"handler": "file_server","root": "/path/to/domain1.tls"}]},
                        {"match": [{"host": ["domain2.tls"]}],"handle": [{"handler": "file_server","root": "/path/to/domain2.tls"}]}
                    ]
                }
            }
        }
    }
}

Thank you for your help.

Best way to learn to use JSON config IMO is to first start from a Caddyfile that does what you want, then use caddy adapt to get the JSON representation.

There’s actually an example in the docs that shows how to do this:

So adapting this Caddyfile…

example.com {
	redir https://www.example.com{uri}
}

www.example.com {
}

Gets you this JSON (with caddy adapt --pretty):

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "www.example.com"
                  ]
                }
              ],
              "terminal": true
            },
            {
              "match": [
                {
                  "host": [
                    "example.com"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "static_response",
                          "headers": {
                            "Location": [
                              "https://www.example.com{http.request.uri}"
                            ]
                          },
                          "status_code": 302
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    }
  }
}
1 Like

Thank you francis, its work

1 Like

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