How to redirect for 404 page

I’ve a react app hosted with istio. In this case if user refresh any page other then the index.html he gets a 404 as route to that path does not exist with istio. Is there a way to redirect traffic to ui pod for any 404?
What I want is if no route match redirect it to ui pod. instead of throwing a 404.

I don’t think it is possible to redirect on 404. However, if you wish to route everything that is not index.html to index.html you can do something like this:

cat <<EOF | kubectl apply -f -
kind: VirtualService
  name: bookinfo
  - "*"
  - bookinfo-gateway
  - match:
    - uri:
        exact: /index.html
    - destination:
        host: productpage
          number: 9080
  - match:
    - uri:
        prefix: /
      uri: /index.html

This example is for book info but you should get the idea. Modify the VirtualService for your Gateway to have a second case that doesn’t match your public page. Have that case take everything (“prefix /”), and redirect to index.html.

1 Like

Thanks @ed.snible.
In my case I’ve couple of routes defined by virtualservice.
for example, I’ve route for anything coming on port 80 with prefix /api, send it to api service on port 8080.
So, here I want if it’s not exact: /index.html or prefix: /api then reroute: /index.html

The match es in the virtual service are processed in order. The last one can be a “catch all” for anything not covered by the others.

In my case, there are multiple virtualservices(files) for a host based on path. In this case the order is not guaranteed. but this helps I can have a single virtualservice with multiple matches and “catch all” in the last.