How to redirect www.domain.tld to domain.tld with Caddy on Rocketchat?

Hi, on Snap + Caddy install for Rocketchat, how to redirect www.domain.tld to domain.tld?

I can access domain.tld but got an error when i typed www.domain.tld - I got this: “404 Site www.domain.tld is not served on this interface”.

Note: on my domain’s dns, there’s only A and CNAME records.

The Caddyfile looks like this:

domain.tld
proxy / localhost:3000 {
websocket
transparent
}

www.domain.tld doesn’t have a definition in that Caddyfile. You’ll need to add another site definition to tell Caddy how to handle requests for it.

For Caddyfiles with multiple site definitions, you’ll need to put each site’s definition inside a parentheses block; see Caddyfile Structure docs:

Your second site definition only needs to contain a redirect to the bare domain.
https://caddyserver.com/docs/redir

To avoid double redirects for HTTP requests, define the behaviour for both HTTP and HTTPS like so:

http://www.example.com, https://www.example.com {
  redir / https://example.com
}

What should I write in the definition?

Sorry, noob here. Tried many things, nothing’s working.

In the Caddyfile, beneath what you’ve already got. The docs have a visual example:

A code example would look like this:

example.com {
  proxy / localhost:3000 {
    websocket
    transparent
  }
}

http://www.example.com, https://www.example.com {
  redir / https://example.com
}
2 Likes

Thanks man, it works.

I understand now what you mean by adding the definition.

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