<p>Using an NGINX Ingresss in Kubernetes, I can't see a way to forward my traffic from non-www to www, or to another domain etc on a per-host basis</p>

<p>I've tried looking in configmap docs but can't see what I need. Maybe it can go in the ingress itself?</p>

<p>I've also seen an example using annotations but this seems to be ingress-wide, so I couldn't have specific redirects per host</p>

Ben Gannaway
<p>Indeed a redirect is possible with a simple annotation:</p>

<ul>
<li><a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#permanent-redirect" rel="noreferrer"><code>nginx.ingress.kubernetes.io/permanent-redirect: https://www.gothereinstead.com</code></a></li>
<li><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></li>
</ul>

<p>But as you mentioned, it's "Ingress" wide and not configurable per host, per domain or even per path. So you'll have to do it yourself through the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#configuration-snippet" rel="noreferrer"><code>ingress.kubernetes.io/configuration-snippet</code></a> annotation, which gives you a great deal of power thanks to regular expressions:</p>

<pre><code>kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: self-made-redirect
  annotations:
    ingress.kubernetes.io/configuration-snippet: |
      if ($host = 'blog.yourdomain.com') {
        return 301 https://yournewblogurl.com;
      }
      if ($host ~ ^(.+)\.yourdomain\.com$) {
        return 301 https://$1.anotherdomain.com$request_uri;
      }
spec:
  rules:
  - host: ...
</code></pre>

<p>If you are not quite used to NGINX, you'll know more about what's possible in the snippet, particularly what is the <code>$host</code> variable right in <a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#variables" rel="noreferrer">the NGINX documentation</a>.</p>
