istio redirect non-www traffic to www

1/23/2020

I am using istioingress gateway. How can I redirect non-www traffic to www?

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
    name: my-gateway
    namespace: some-config-namespace
    spec:
    selector:
        app: my-gateway-controller
    servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
        hosts:
        - uk.bookinfo.com
        - eu.bookinfo.com
        - www.uk.bookinfo.com
        - www.eu.bookinfo.com
        tls:
        httpsRedirect: true # sends 301 redirect for http requests
    - port:
        number: 443
        name: https-443
        protocol: HTTPS
        hosts:
        - uk.bookinfo.com
        - eu.bookinfo.com
        - www.uk.bookinfo.com
        - www.eu.bookinfo.com
        tls:
        mode: SIMPLE # enables HTTPS on this port
        serverCertificate: /etc/certs/servercert.pem
        privateKey: /etc/certs/privatekey.pem

Currently, I am able to access the website using both endpoints. But, I want to redirect all traffic from non-www to www.

-- Ronak Patel
istio
kubernetes

2 Answers

1/24/2020

Have you tried redirect feature of Istio?

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: server-vs
spec:
  hosts:
  - mysite.com
  gateways:
  - my-gateway
  http:
  - match:
    - uri:
        exact: /
    redirect:
      uri: /
      authority: www.mysite.com

Also you could have done the redirection at DNS level. But I think some domain providers don't support it. Godaddy, for example, does.

-- suren
Source: StackOverflow

1/23/2020

Istio Gateway receives the traffic, and the routing from there will be handled by VirtualService configuration. For your non-www to www traffic routing, the same question was raised in Istio discussion forum, so that may be of your help.

https://discuss.istio.io/t/simply-redirecting-non-www-to-www/3370

As to getting all the traffic, you may want to use a wildcard in the hosts definition in the Gateway configuration (ref: https://istio.io/docs/reference/config/networking/gateway/#Server).

-- Ryota
Source: StackOverflow