<p>I want to redirect the main domain in Kubernetes ingress Nginx but would like subdomains to get applied to the redirected main domain.</p>
<p>Here is the example website: <strong>my.website.com</strong></p>
<p>Inside this website there are many subdomains which are /abc, /def, /abc/aa, /def/bb, etc.</p>
<p>For my Kubernetes ingress Nginx, I want to redirect <em>my.website.com</em> to <em>this.website.com</em>
and also would like all subdomains to redirect to <em>this.website.com</em></p>
<p>Here is the example that I want:</p>
<p><code>my.website.com/abc</code> to <code>this.website.com/abc</code></p>
<p>Here is the redirect ingress yaml that I tried:</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: https://this.website.com
  name: redirect-subdomains-too
spec:
  rules:
  - host: my.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: /
</code></pre>
<p>Obviously, when I try <code>my.website.com/abc</code> OR <code>my.website.com/abc/aa</code> this will redirect to <code>this.website.com</code> not to <code>this.website.com/abc</code> OR <code>this.website.com/abc/aa</code>.</p>
<p>How can I script this ingress?</p>

JShine
<p>The permanent redirect is like 'hard code' for everything. You probably want <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">rewrite</a> and/or a <code>server-snippet</code>. Then you can have multiple host rules for each of the hosts:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/server-snippet: |
        if ($host ~ &quot;my.website.com|my2.website.com&quot;)
        {
            rewrite ^ https://this.website.com$request_uri permanent;
        }
  name: redirect-subdomains-too
spec:
  rules:
  - host: my.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)
  - host: my2.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)
  - host: this.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)

</code></pre>
<p>You can always check the config by running:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl exec -t &lt;nginx-ingress-pod&gt; cat nginx.conf
</code></pre>
