<p>How can I configure <code>nginx.ingress.kubernetes.io/rewrite-target</code> and <code>spec.rules.http.paths.path</code> to satisfy the following URI patterns?</p>

<pre><code>/aa/bb-aa/coolapp
/aa/bb-aa/coolapp/cc
</code></pre>

<p><strong>Legend</strong>:</p>

<ul>
<li><strong>a</strong> = Any letter between a-z. Lowercase. Exactly 2 letters - no more,
no less.</li>
<li><strong>b</strong> = Any letter between a-z. Lowercase. Exactly 2 letters - no more, no less.</li>
<li><strong>c</strong> = any valid URI character. Lowercase. Of variable length - think slug.</li>
</ul>

<p><strong>Example URI:s that should match the above pattern</strong>:</p>

<pre><code>/us/en-us/coolapp
/us/en-us/coolapp/faq
/us/en-us/coolapp/privacy-policy
</code></pre>

<p><strong>Attention</strong></p>

<p>Starting in Version 0.22.0, ingress definitions using the annotation <code>nginx.ingress.kubernetes.io/rewrite-target</code> are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.</p>

<p><strong>Note</strong></p>

<p>Captured groups are saved in numbered placeholders, chronologically, in the form <code>$1</code>, <code>$2</code> ... <code>$n</code>. These placeholders can be used as parameters in the rewrite-target annotation.</p>

<p><strong>References</strong>:</p>

<ol>
<li><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="noreferrer">https://kubernetes.github.io/ingress-nginx/examples/rewrite/</a></li>
<li><a href="https://github.com/kubernetes/ingress-nginx/pull/3174" rel="noreferrer">https://github.com/kubernetes/ingress-nginx/pull/3174</a></li>
</ol>

PussInBoots
<p>The <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rewrite" rel="noreferrer"><code>nginx.ingress.kubernetes.io/rewrite-target</code></a> annotation is used to indicate the target URI where the traffic must be redirected. As per how I understand your question, you only want to match the URI patterns that you specified without redirecting the traffic. In order to achieve this, you can set the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#use-regex" rel="noreferrer"><code>nginx.ingress.kubernetes.io/use-regex</code></a> annotation to <code>true</code>, thus enabling regular expressions in the <code>spec.rules.http.paths.path</code> field.  </p>

<p>Let's now take a look at the regex that you will need to match your URI patterns.
First of all, the regex engine used by ingress-nginx <a href="https://stackoverflow.com/questions/23968992/how-to-match-a-regex-with-backreference-in-go">doesn't support backreferences</a>, therefore a regex like <a href="https://regex101.com/r/M48Do1/1" rel="noreferrer">this one</a> will not work. This is not a problem as you can match the <code>/aa-bb/aa</code> part without forcing the two <code>aa</code>s to be equal since you will —presumably— still have to check the correctness of the URI later on in your service (e.g <code>/us/en-us</code> may be accepted whereas <code>/ab/cd-ab</code> may not).</p>

<p>You can use <a href="https://regex101.com/r/M48Do1/2" rel="noreferrer">this regex</a> to match the specified URI patterns:</p>

<pre><code>/[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/.*)?
</code></pre>

<p>If you want to only match URL slugs in the <code>cc</code> part of the pattern you specified, you can use this regex instead:</p>

<pre><code>/[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/[a-z0-9]+([a-z0-9]+)*)?
</code></pre>

<p>Lastly, as the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#use-regex" rel="noreferrer"><code>nginx.ingress.kubernetes.io/use-regex</code></a> enforces case insensitive regex, using <code>[A-Z]</code> instead of <code>[a-z]</code> would lead to the same result.</p>

<hr>

<p>Follows an ingress example definition using the <code>use-regex</code> annotation:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-regex
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: test.com
    http:
      paths:
      - path: /[a-z]{2}/[a-z]{2}-[a-z]{2}/coolapp(/.*)?
        backend:
          serviceName: test
          servicePort: 80
</code></pre>

<p>You can find more information about Ingress Path Matching in the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/" rel="noreferrer">official user guide</a>.</p>
