<p>There are multiple same pods running in one cluster but different namespaces. This is the web application running in Kubernetes. I have the URL <code>&lt;HOSTNAME&gt;:&lt;PORT&gt;/context/abc/def/.....</code>. I want to redirect to particular service based on the context. Is there a way i can achieve it using ingress controller ? Or Is there any way i can achieve it using different ports through ingress ?</p>

<p>My web application works fine if the URL is <code>&lt;HOSTNAME&gt;:&lt;PORT&gt;/abc/def/.....</code>. Since i have to access the different pods using the same URL, I am adding context to it. Do we have any other way to achieve this use case ?</p>

Sunil Gudivada
<p>You can do that with  <code>rewrite-target</code>. In example below i used  <code>&lt;HOSTNAME&gt;</code>  value of  <code>rewrite.bar.com</code>  and  <code>&lt;PORT&gt;</code>  with value  <code>80</code>.</p>

<pre><code>apiVersion: extensions/v1beta1
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: context-service
          servicePort: 80
        path: /context1(/|$)(.*)
      - backend:
          serviceName: context-service2
          servicePort: 80
        path: /context2(/|$)(.*)       
</code></pre>

<p>For example, the ingress definition above will result in the following rewrites:</p>

<p><code>rewrite.bar.com/context1</code>  rewrites to  <code>rewrite.bar.com/</code> for context 1 service.</p>

<p><code>rewrite.bar.com/context2</code>  rewrites to  <code>rewrite.bar.com/</code> for context 2 service.</p>

<p><code>rewrite.bar.com/context1/new</code>  rewrites to  <code>rewrite.bar.com/new</code> for context 1 service.</p>

<p><code>rewrite.bar.com/context2/new</code>  rewrites to  <code>rewrite.bar.com/new</code> for context 2 service.</p>
