<p>I am trying to redirect my domain 'www.test.example.com' to <code>test.example.com</code></p>

<p>in ingress i have added annotation </p>

<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'www.test.wotnot.io' ) {
          rewrite ^/(.*)$ https://app.test.wotnot.io/$1 permanent;
      }
</code></pre>

<p>it's not working as expected.</p>

<p>For testing i have try this</p>

<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'test.example.com' ) {
          rewrite ^/(.*)$ https://google.com/$1 permanent;
      }
</code></pre>

<p>which is working fine.</p>

<p>My site is working on <code>test.example.com</code> and ssl certificate.</p>

<p>Whole ingress</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: wordpress-staging
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
    #nginx.ingress.kubernetes.io/configuration-snippet: |
      #if ($host = 'www.test.wotnot.io' ) {
        #  rewrite ^/(.*)$ https://test.example.io/$1 permanent;
      #}
  name: wordpress-staging-ingress
spec:
  rules:
  - host: test.example.io
    http:
      paths:
      - backend:
          serviceName: wordpress-site
          servicePort: 80
        path: /
  tls:
  - hosts:
    - test.example.io
    secretName: wordpress-staging
</code></pre>

<p>Ingress has an annotation <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#redirect-fromto-www" rel="noreferrer"><code>nginx.ingress.kubernetes.io/from-to-www-redirect: "true"</code></a> which already handle this:</p>

<blockquote>
  <p>In some scenarios is required to redirect from <code>www.domain.com</code> to
  <code>domain.com</code> or vice versa. To enable this feature use the annotation
  <code>nginx.ingress.kubernetes.io/from-to-www-redirect: "true"</code></p>
  
  <p><strong>Attention</strong>: For HTTPS to HTTPS redirects is mandatory the SSL Certificate defined in the Secret, located in the TLS section of Ingress, contains both FQDN in the common name of the certificate.</p>
</blockquote>

<p>It's better that you use it instead of fighting/tweaking the <code>configuration-snippet</code> annotation.</p>
