title: Kubernetes Ingress - How to rewrite all non-www traffic to www subdomain?
body: <p>I have a Kubernetes Ingress resource where I'm trying to redirect all non-www traffic to www subdomain for url canonicalization. So all traffic on <code>example.com</code> should be rewritten to <code>www.example.com</code>. I can't seem to figure out how to use the <a href="https://github.com/kubernetes/contrib/tree/master/ingress/controllers/nginx/examples/rewrite" rel="nofollow noreferrer">Ingress rewrite example</a> properly to achieve this.</p>
  
  <p>My Ingress (JSON format):</p>
  
  <pre><code>{
    "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
                }
              }
            ]
          }
        }
      ]
    }
  }
  </code></pre>