<p>I have a kubernetes cluster with nginx-ingress and I try to redirect <a href="http://test.domain.com" rel="nofollow noreferrer">http://test.domain.com</a> to <a href="http://www.test.domaine.com" rel="nofollow noreferrer">http://www.test.domaine.com</a> with nginx.ingress.kubernetes.io/from-to-www-redirect annotation.</p>
<p>But it doesn't seem to work.</p>
<p>My ingress ressource :</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/from-to-www-redirect: &quot;true&quot;
spec:
  rules:
  - host: www.test.domain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: app
          servicePort: 8080
  - host: test.domain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: app
          servicePort: 8080
</code></pre>
<p>I have tried many configuration but I can't make it work. The nginx.ingress.kubernetes.io/from-to-www-redirect annotation have no effect!</p>

<p>This is a community wiki answer. Feel free to expand it.</p>
<p>In order to make it work you also need to use the <code>nginx.ingress.kubernetes.io/configuration-snippet</code> <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#configuration-snippet" rel="nofollow noreferrer">annotation</a>. For example:</p>
<pre><code>nginx.ingress.kubernetes.io/configuration-snippet: |
  if ($host = 'example.com' ) {
    rewrite ^ https://www.example.com$request_uri permanent;
  }
</code></pre>
<p>As already mentioned by @apoorvakamath in the comments, you can refer to <a href="https://www.informaticsmatters.com/blog/2020/06/03/redirecting-to-www.html" rel="nofollow noreferrer">this guide</a> for a step by step detailed example:</p>
<blockquote>
<p>The snippet is the clever aspect fo the solution. It allows you to add
dynamic configuration to the ingress controller. We use it to detect
the use of the non-www form of the URL and then simply issue a
re-write that is pushed out (and back via your domain) to the www
form.</p>
</blockquote>
