Rewrite all non www traffic to www subdomain

0 votes

I'm trying to redirect all non-www traffic to www subdomain for url canonicalization. So all traffic on example.com should be rewritten to . I can't seem to figure out how though.

My Ingress:

{
  "apiVersion": "extensions/v1beta1",
  "kind": "Ingress",
  "metadata": {
    "name": "example-staging",
    "annotations": {
      "ingress.kubernetes.io/rewrite-target": "/",
      "kubernetes.io/ingress.global-static-ip-name": "example-static-ip"
    }
  },
  "spec": {
    "rules": [
      {
        "host": "www.example.nl",
        "http": {
          "paths": [
            {
              "path": "/",
              "backend": {
                "serviceName": "example-service",
                "servicePort": 80
              }
            }
          ]
        }
      }
    ]
  }
}
Oct 1, 2018 in Kubernetes by lina
• 8,160 points
37 views

1 answer to this question.

0 votes

Try adding a second Ingress for example.nl that uses the ingress.kubernetes.io/configuration-snippet annotation to add the 301.

{
  "apiVersion": "extensions/v1beta1",
  "kind": "Ingress",
  "metadata": {
    "name": "example-staging-wwwredir",
    "annotations": {
      "ingress.kubernetes.io/rewrite-target": "/",
      "ingress.kubernetes.io/configuration-snippet": "return 301 $scheme://www.example.nl$request_uri;"
    }
  },
  "spec": {
    "rules": [
      {
        "host": "example.nl",
        "http": {
          "paths": [
            {
              "path": "/",
              "backend": {
                "serviceName": "example-service",
                "servicePort": 80
              }
            }
          ]
        }
      }
    ]
  }
}
answered Oct 1, 2018 by Kalgi
• 45,970 points

Related Questions In Kubernetes

0 votes
1 answer

What all packages do i need to install to use kubernetes on my machine?

Install below packages on all of your ...READ MORE

answered Jul 11 in Kubernetes by Sirajul
• 40,990 points
106 views
0 votes
1 answer

how to drain the traffic from a pod?

When we take the node for maintenance, ...READ MORE

answered Jul 17 in Kubernetes by Sirajul
• 40,990 points
39 views
0 votes
1 answer
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17 in Others by anonymous
3,452 views
+3 votes
1 answer
0 votes
1 answer

How to rewrite all non-www traffic to www subdomain?-kubernetes ingress

The ingress.kubernetes.io/rewrite-target is used for rewriting the request URI, ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 45,970 points
864 views