How to migrate Go projects to a new domain using Caddy

Donate

I recently migrated from rocketnine.space to rocket9labs.com, including a Forgejo instance. I have a number of projects, including many Go projects. I searched for a way to redirect to a new domain, while still allowing go get commands to work when referencing the old domain. I found this thread which provides an example of how to redirect a single package. The snippet in this guide redirects all packages on a domain.

Add the following to your Caddyfile, replacing code.rocketnine.space with your old domain and code.rocket9labs.com with your new domain:

code.rocketnine.space {
  route * {
    @goget query go-get=1
    respond @goget `<meta name="go-import" content="{host}{path} git https://code.rocket9labs.com{path}">`
    redir https://code.rocket9labs.com{path}
  }
}

This will not only redirect all normal requests from the old domain to the new domain, it will also redirect go get requests in a way that allows commands using the old domain to continue to work.