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

0 votes

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 example.com should be rewritten to www.example.com. I can't seem to figure out how to use the Ingress rewrite example properly to achieve this.

My Ingress (JSON format):

{
  "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
              }
            }
          ]
        }
      }
    ]
  }
}
Sep 11, 2018 in Kubernetes by lina
• 8,220 points
3,432 views

1 answer to this question.

0 votes

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

It looks like from your link you're using the nginx ingress controller. You can get the effect you want by 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 Sep 11, 2018 by Kalgi
• 52,360 points

Related Questions In Kubernetes

0 votes
1 answer

How to use gravitational teleport in a container/kubernetes environment?

You can use teleport to augment kubernetes ...READ MORE

answered Jun 28, 2018 in Kubernetes by ajs3033
• 7,300 points
2,226 views
0 votes
1 answer

How do I sign-in to kubernetes dashboard?

Since version 1.7 Dashboard uses more secure ...READ MORE

answered Sep 7, 2018 in Kubernetes by DareDev
• 6,890 points
2,041 views
0 votes
1 answer

Kubernetes ingress IP is set to docker0 ip

The issue here was the kubelet configuration. ...READ MORE

answered Sep 10, 2018 in Kubernetes by Kalgi
• 52,360 points
531 views
0 votes
2 answers

Not able to expose port 80 on the host, kubernetes ingress

I was facing the same error. The nginix ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 52,360 points
1,745 views
+1 vote
1 answer
0 votes
1 answer

Kubernetes nginx-ingress TLS issue

You have to create a secret named test-secret. ➜ ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 52,360 points
1,755 views
0 votes
3 answers

Error while joining cluster with node

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

answered Jan 17, 2019 in Others by anonymous
14,585 views
0 votes
1 answer

Rewrite all non www traffic to www subdomain

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

answered Oct 1, 2018 in Kubernetes by Kalgi
• 52,360 points
443 views
0 votes
2 answers

Deny access to some specific paths while using kubernetes ingress

Try to create two Ingresses first by default ...READ MORE

answered Sep 21, 2018 in Kubernetes by Nilesh
• 7,050 points
11,507 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP