This is probably a matter of opinion but I always prefer redirecting www to non-www on domain names. You'll notice this domain (ben-knox.com) redirects from www.ben-knox.com to ben-knox.com. Back when I was learning about web servers and working with Apache or nginx on a server this was not too hard to accomplish with a redirect directive. When I moved to using Kubernetes I wasn't working directly with servers so this method didn't work anymore, now I'm setting up ingress routes and don't always directly work with the web server. So we need a new way right? The following is an example of redirecting your ingress route matching www.your-domain.name to your-domain.name or vice-versa and a little bit of explanation on what's going on.
Pre-requisites
This article assumes you have some knowledge of ingress in Kubernetes and cert-manager. I am also assuming that your DNS is set up correctly for both @ and www routes in your zone file (you should be able to google how to for your provider).
I highly recommend transferring to spaceship.com for your domains. I was using google domains but after Squarespace bought it I decided to transfer my domains, they have great deals and their web UI is also easier to use.
Redirecting WWW (or to WWW)
Some background here, when you're working with a managed Kubernetes service generally you set up a load balancer for your cluster or it gets installed automatically for you when you create a service that requires one. One option for connecting the load balancer inside your cluster is via a service and it usually looks something like this:
This will only route traffic to one service in your cluster per load balancer though, so you'd need to create multiple load balancers for every service you want to expose in the cluster. Which is why you need an ingress controller, with an ingress controller you can route traffic to any number of services within your cluster on a single load balancer.
First you need to install an ingress controller, this article uses the nginx ingress controller. Once that's in place, you need to create an ingress configuration to route your traffic, below is an example of this:
This example (the ingress configuration above) creates a route for example-app.com, but there's a problem. If you were to visit www.example-app.com you wouldn't be routed to the container, to redirect you have to add the following annotation:
If you are using cert-manager, you also have to add www.example-app.com to your tls list:
And if you're wanting to use www instead of non-www, you just update the host value under rules to www.example-app.com. Read more about the annotation here.
And that's all! If you're familiar with Apache or nginx, this is a little bit different, but t's a pretty simple set up and easy to understand! Happy coding ✌️