Simply redirecting non-www to www

Since this is so common, seems this should be easy to do in VirtualService, but having a hard time doing simple HTTPRedirect on non-www domain to www. Here’s what I’ve got (appropriate Gateway is passing the traffic):

  gateways:
  - example-gateway
  hosts:
  - example.net
  http:
  - match:
    - headers:
        host:
          regex: "^example.net"
    redirect:
      authority: www.example.net

Tried a couple of variations with match authority, but just can’t get the redirect to work. Where am I going wrong?

Resolved, and good news it is as trivial as it should be, just note the spec.hosts section in this simplified example, where you can add all of the domains you want a particular VirtualService to handlle.

kind: VirtualService
metadata:
  name: www-example
  namespace: example
  version: v3
spec:
  gateways:
  - example-gateway
  hosts:
  - example.net
  - www.example.net
  http:
  - match:
    - authority:
        exact:
          example.net
    redirect:
      authority: www.example.net
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        port:
          number: 80
        host: nginx-example-service
1 Like

I did a little refactoring. Source snippet that works for me

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
  name: example-virtual-service
  namespace: prod
spec:
  gateways:
  - example-gateway
  hosts:
  - example.com
  - www.examle.com
  http:
  - match:
    - authority:
        exact:
          example.com
    redirect:
      authority: www.example.com
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: nginx
        port:
          number: 80