<p>I have read in ingress-nginx documentation that the rewrite is being performed thanks to an annotation like this:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)
</code></pre>
<p>I have a case where I have multiple hosts and I want URL rewriting for some particular paths only:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt
    nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;
  name: ingress
spec:
  rules:
    - host: somehost.westeurope.cloudapp.azure.com
      http:
        paths:
        - path: /rest-smtp-sink # I want to rewrite this path
          pathType: Prefix
          backend:
            service:
              name: rest-smtp-sink-svc
              port:
                number: 80
        - path: /backend # This one too
          pathType: Prefix
          backend:
            service:
              name: server-svc
              port:
                number: 80
        - path: / # But not this one
          pathType: Prefix
          backend:
            service:
              name: client-svc
              port:
                number: 80
</code></pre>
<p>However, the annotation seems to be global. How do I enable URL rewriting for some paths only?</p>

<p>I managed to get the desired result with this configuration:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt
    nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: ingress
spec:
  rules:
    - host: somehost.westeurope.cloudapp.azure.com
      http:
        paths:
        - path: /rest-smtp-sink(/|$)(.*)
          pathType: Prefix
          backend:
            service:
              name: rest-smtp-sink-svc
              port:
                number: 80
        - path: /backend(/|$)(.*)
          pathType: Prefix
          backend:
            service:
              name: server-svc
              port:
                number: 80
        - path: /()(.*)
          pathType: Prefix
          backend:
            service:
              name: client-svc
              port:
                number: 80
</code></pre>
<p>As the <code>nginx.ingress.kubernetes.io/rewrite-target</code> annotation is global, I've used <code>/$2</code> as the rewrite target and <code>/()(.*)</code> as a noop for the root path.</p>
