<p>I have an ingress controller working for UI container service and backend container service. my ingress configuration is as follows:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
 name: testapp
 annotations:
  rules:
  - host: test1.example.com
    http:
      paths:
      - path: /static
        backend:
          serviceName: ui-service
          servicePort: 80
      - path: /apicall
        backend:
          serviceName: backend-service
          servicePort: 8080
</code></pre>
<p>Which is working fine. Now I need to forward this ingress URL if it contains <code>?</code>.</p>
<p>For eg, if url is <code>test1.example.com/?productid=10001</code> I need to add static before this <code>?</code> and forward it to <code>test1.example.com/static?productid=10001</code>.</p>
<p>Is this behavior possible through below annotations?</p>
<pre><code> nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>
<p>If yes, how to have that regex kind of url where if <code>?</code> is present in the url followed by any string/characters, add <code>static</code> keyword before it?</p>

<p>First of all look at the YAML below to understand how works <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite" rel="nofollow noreferrer">rewrite rule</a>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80
</code></pre>
<blockquote>
<p>In this ingress definition, any characters captured by  <code>(.*)</code>  will be assigned to the placeholder  <code>$2</code>, which is then used as a parameter in the  <code>rewrite-target</code>  annotation.</p>
<p>For example, the ingress definition above will result in the following rewrites:</p>
<ul>
<li><code>rewrite.bar.com/something</code>  rewrites to  <code>rewrite.bar.com/</code></li>
<li><code>rewrite.bar.com/something/</code>  rewrites to  <code>rewrite.bar.com/</code></li>
<li><code>rewrite.bar.com/something/new</code>  rewrites to  <code>rewrite.bar.com/new</code></li>
</ul>
</blockquote>
<p>In your case, you will need to follow a similar procedure. But first, I suggest you do a separate ingress for each path. This will help you keep order later and additionally you will have an independent ingress for each path. Look at <a href="https://serverfault.com/questions/1065154/nginx-ingress-rewrite-rule/1065176#1065176">this question</a> to see why and how you should create a separate ingress.</p>
<blockquote>
<p>Now I need to forward this ingress URL if it contains ?. for eg if URL is test1.example.com/?productid=10001 i need to add static before this ? and forward it to test1.example.com/static?productid=10001.</p>
</blockquote>
<p>You will need to create an appropriate capture group. For the data you presented it will look like this:</p>
<pre class="lang-yaml prettyprint-override"><code> - path: /(/|$)(.*)
</code></pre>
<p>and the annotation should be like this:</p>
<pre class="lang-yaml prettyprint-override"><code>nginx.ingress.kubernetes.io/rewrite-target: /static/$2
</code></pre>
<p>In this case, everything that gets captured from the URL after the <code>/</code> character will be rewritten to the new address, which will look like <code>/static</code> and the address that was before. There should be no problem with the question mark.</p>
<p>If you need to create a regex that will act on a question mark, you will need to prefix it with a <code>\</code> character, which is described <a href="https://www.regular-expressions.info/characters.html#special" rel="nofollow noreferrer">here</a>.</p>
<p>Also, consider that you are currently targeting root <code>/</code> path and want to create 2 ingresses. I also suggest creating appropriate endpoints depending on the situation, so that you can easily create 2 different ingresses responsible for directing different traffic.</p>
