<p>To answer my question, to achieve my goal the correct way of writing a <code>virtualservice</code> according to the Istio <a href="https://istio.io/latest/docs/reference/config/networking/virtual-service/#RegexRewrite" rel="nofollow noreferrer">reference documentation</a> is as below:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uriRegexRewrite:
        match: /captcha-srv(/|$)(.*)
        rewrite: /\2
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - match:
    - uri:
        prefix: /
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80
</code></pre>
<p>It's essential to know how to read and understand the reference manuals in such cases, e.g. in this case <code>HTTPRewite</code> is a subsection of <code>virtualservice</code> and therefore has an indentation, then, if you use the <code>uriRegexRewrite</code> you'll have to refer to the <code>RegexRewrite</code> section and use the <code>match</code> and <code>rewrite</code> directives with an indentation.</p>

CC BY-SA 4.0
2024-01-21T06:04:02.127Z
2024-01-21T06:04:02.127Z
Sinux
<p>I was waiting on the Regex Rewrite based on Capture Groups for a long time and it was finally merged in 1.19.4. I cannot find much documentation regarding how to use the uriRegexRewrite and my attempts to make it work all failed so far.</p>
<p>I need the VirtualService to capture something like <code>/captcha-srv(/|$)(.*)</code> and rewrite it to <code>/$2</code></p>
<p>My current Virtual Service looks like this:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uri: /
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80
</code></pre>
<p>This setup rewrites whatever you write after <code>captcha-srv</code> to my microservice's <code>/</code> however, I need it to send <code>/captcha-srv</code> to <code>/</code> and e.g. <code>/captcha-srv/v1</code> to <code>/v1</code>. In Nginx Ingress, this is easily done, but apparently, this feature was recently added to Istio, and using the documentation wasn't much help except that the directive used to achieve this is <code>uriRegexrewrite</code>.</p>
<p>Any help?</p>

<kubernetes><networking><istio>
How to use uriRegexRewrite in Istio VirtualService?
Sinux
# How to use uriRegexRewrite in Istio VirtualService?

(Asked by: username_0 on 2024-01-06)

I was waiting on the Regex Rewrite based on Capture Groups for a long time and it was finally merged in 1.19.4. I cannot find much documentation regarding how to use the uriRegexRewrite and my attempts to make it work all failed so far.
I need the VirtualService to capture something like /captcha-srv(/|$)(.*) and rewrite it to /$2
My current Virtual Service looks like this:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uri: /
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80

This setup rewrites whatever you write after captcha-srv to my microservice's / however, I need it to send /captcha-srv to / and e.g. /captcha-srv/v1 to /v1. In Nginx Ingress, this is easily done, but apparently, this feature was recently added to Istio, and using the documentation wasn't much help except that the directive used to achieve this is uriRegexrewrite.
Any help?

**Tags:** kubernetes, networking, istio


---

## 1 answers

### Answer by username_0 on 2024-01-21. Score: 1 | Accepted answer

To answer my question, to achieve my goal the correct way of writing a virtualservice according to the Istio reference documentation is as below:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  labels:
    app.kubernetes.io/instance: authserver-webapi
  name: authserver-webapi
  namespace: istio-full
spec:
  gateways:
  - authserver-webapi
  hosts:
  - auth.example.com
  http:
  - match:
    - uri:
        regex: /captcha-srv(/|$)(.*)
    rewrite:
      uriRegexRewrite:
        match: /captcha-srv(/|$)(.*)
        rewrite: /\2
    route:
    - destination:
        host: captcha
        port:
          number: 80
  - match:
    - uri:
        prefix: /
  - route:
    - destination:
        host: authserver-webapi
        port:
          number: 80

It's essential to know how to read and understand the reference manuals in such cases, e.g. in this case HTTPRewite is a subsection of virtualservice and therefore has an indentation, then, if you use the uriRegexRewrite you'll have to refer to the RegexRewrite section and use the match and rewrite directives with an indentation.


---

