V2: strip subdomain www?

1. My Caddy version (caddy -version):

(devel)

2. How I run Caddy:

sudo caddy start --config caddy.conf

a. System environment:

ubuntu

b. Command:

n/a

c. Service/unit/compose file:

n/a

d. My complete Caddyfile:

{
    "logging": {
        "sink": {
            "writer": {
                "output": "file",
                "filename": "/var/log/caddy/sink.log"
            }
        },
        "logs": {
            "default": {
                "writer": {
                    "output": "file",
                    "filename": "/var/log/caddy/caddy.log"
                },
                "encoder": {
                    "format":"console"
                },
                "level": "info",
                "include": [],
                "exclude": []
            }
        }
    },
    "apps": {
        "http": {
            "servers": {
                "myserver": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                         {
                            "match": [
                                {
                                    "host": [
                                        "bitwarden.sethammons.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "transport": {
                                      "protocol": "http",
                                      "tls": {}
                                    },
                                    "upstreams": [
                                        {
                                            "dial": "bitwarden.sethammons.com:5443",
                                            "max_requests": 1000
                                        }
                                    ]
                                }
                            ]
                        },

                        {
                            "match": [
                                {
                                    "host": [
                                        "sethammons.com", "www.sethammons.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "file_server",
                                    "root": "/home/seth/projects/me/site/public"
                                }
                            ]
                        },
                        {
                            "match": [
                                {
                                    "host": [
                                        "grzlybr.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "reverse_proxy",
                                    "upstreams": [
                                        {
                                            "dial": "localhost:1414",
                                            "max_requests": 1000
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

3. The problem I’m having:

I want https://www.sethammons.com to redirect to https://sethammons.com (ie, strip the www). Currently, I have it serving the www subdomain but would prefer it to redirect to not have the www.

I am not able to understand the way to do this based on the documentation. I imagine the redir handler has some way to send the given request to a different match. I just don’t grok it.

4. Error messages and/or full log output:

n/a

5. What I already tried:

I read through the docs and see there are redir and rewrite options, but I don’t see how to convert them to json and add them to my caddy file. It seems like redir is the right one, but I don’t see how to say “redir this matcher to this other matcher.”

6. Links to relevant resources:

I’m using the Caddyfile adapter, with a command like “/usr/local/bin/caddy run --config /etc/caddy/Caddyfile --adapter caddyfile”, while you seem to be using straight JSON. Also, I’m doing the reverse of what you’re doing, redirecting non-www traffic to www. That said, here’s my current v2 Caddyfile, which works for me:

{
	storage file_system {
		root	/etc/caddy/storage
	}
	experimental_http3
}

www.sunrisemovement.dev {
	root * /srv/sunrisemovement.dev/public/
	encode gzip zstd brotli
	file_server
}

sunrisemovement.dev {
	redir https://www.sunrisemovement.dev
}

Any idea how to translate that to json?

Run it through caddy adapt -config Caddyfile -pretty:

{
	"storage": {
		"module": "file_system",
		"root": "/etc/caddy/storage"
	},
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":443"
					],
					"routes": [
						{
							"match": [
								{
									"host": [
										"www.sunrisemovement.dev"
									]
								}
							],
							"handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"handler": "vars",
													"root": "/srv/sunrisemovement.dev/public/"
												},
												{
													"handler": "subroute",
													"routes": [
														{
															"handle": [
																{
																	"encodings": {
																		"brotli": {},
																		"gzip": {},
																		"zstd": {}
																	},
																	"handler": "encode"
																}
															]
														},
														{
															"handle": [
																{
																	"handler": "file_server",
																	"hide": [
																		"Caddyfile"
																	]
																}
															]
														}
													]
												}
											]
										}
									]
								}
							]
						},
						{
							"match": [
								{
									"host": [
										"sunrisemovement.dev"
									]
								}
							],
							"handle": [
								{
									"handler": "subroute",
									"routes": [
										{
											"handle": [
												{
													"handler": "static_response",
													"headers": {
														"Location": [
															"https://www.sunrisemovement.dev"
														]
													},
													"status_code": 307
												}
											]
										}
									]
								}
							]
						}
					],
					"experimental_http3": true
				}
			}
		}
	}
}
3 Likes

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