Many people type www
in front of the domain name out of habit. At the same time, others omit it. This could cause problems.
As a webmaster, it is my job to make sure that neither of these groups run into an error. However, I don’t want to support both versions for every single page search engine optimization reasons.
Multiple domains for the same content is bad for Your SEO ranking. Search engines count links pointing to Your URLs. However, they don’t know whether www.crossquiz.net and crossquiz.net are the same website, so they treat them as different entities. If You have 5 backlinks to www and 5 backlinks to `non-www, another site with 7 backlinks will rank higher than Your two URLs, although You actually have 10 total. According to Semrush, each subdomain has its own ranking.
The solution to this problem is to redirect users from one version to the other with an “HTTP 301 redirect” status code. This will A) cause the browser to change the URL so when they copy the URL, they will copy the same one and B) will tell the search engine that these two links are identical.
In order to do this with the NGINX Ingress Controller You can use the from-to-www-redirect
annotation in your ingress YAML file:
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
Keep in mind that You will still need an HTTPS certificate for both versions. However, in Your ingress YAML file, its enough to just define the path that You want. If You define both paths, then the redirect won’t work.
So in my example, I want people who enter www.crossquiz.net
to be redirected to the domain crossquiz.net
. Here is my full ingress rule:
ingress.yaml
apiversion: networking.k8s.io/v1
kind: ingress
metadata:
name: crossquiz-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
cert-manager.io/cluster-issuer: letsencrypt
spec:
tls:
- hosts:
- crossquiz.net
- www.crossquiz.net
secretname: tls-secret
rules:
- host: crossquiz.net
http:
paths:
- backend:
service:
name: crossquiz
port:
number: 80
path: /(.*)
pathtype: implementationspecific
This might be a bit different in Your case, but the important part is that You have:
- the annotation
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
must be set - the HTTPS host must be listed so that the certificate can be acquired (I use LetsEncrypt)
- the path that You want people to use must be set (in my case crossquiz.net)
Now whenever someone visits www.crossquiz.net
, they will get redirected to crossquiz.net
without noticing. Their browser will show it accordingly.
Could I help? Buy me a drink ! 💙