Search code examples
kuberneteshttp-redirectkubernetes-ingressnginx-ingress

Redirect from www to non www duplicates query params kubernetes


I am trying to redirect from https://example.nl to https://www.example.nl. This works perfectly. However, when I add query params, the query params get duplicated.

For example, whenever I go to example.nl?test=a, it redirects to wwww.example.nl?test=a?test=a.

How do I prevent this duplication of query params?

I use kubernetes and digital ocean. My kubernetes ingress file looks as follows:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    service.beta.kubernetes.io/do-loadbalancer-name: "example"
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/backend-protocol: HTTP
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'example.nl' ) {
        rewrite ^ https://www.example.nl$request_uri permanent;
      }
    nginx.ingress.kubernetes.io/server-snippet: |
      gzip on;
      gzip_disable "msie6";
      gzip_vary on;
      gzip_proxied any;
      gzip_comp_level 6;
      gzip_buffers 16 8k;
      gzip_http_version 1.1;
      gzip_min_length 256;
      gzip_types
        application/atom+xml
        application/geo+json
        application/javascript
        application/x-javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rdf+xml
        application/rss+xml
        application/xhtml+xml
        application/xml
        font/eot
        font/otf
        font/ttf
        image/svg+xml
        text/css
        text/javascript
        text/plain
        text/xml;
spec:
  tls:
    - hosts:
        - www.example.nl
        - example.nl
      secretName: main-example-tls
  rules:
    - host: www.example.nl
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: front-end
                port:
                  number: 3000
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: back-end
                port:
                  number: 8000
    - host: example.nl
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: front-end
                port:
                  number: 3000
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: back-end
                port:
                  number: 8000

If any additional informations is required. Please let me know.


Solution

  • try something like

    annotations:
            nginx.ingress.kubernetes.io/configuration-snippet: |
             rewrite / https://test.app.example.com$uri permanent;
    

    You can also refer my article : https://medium.com/@harsh.manvar111/kubernetes-ingress-domain-redirect-4595e9030a2c