<p>I'm using NGINX (v4.5.2) on a Kubernetes cluster, and I'm trying to configure it to redirect non-www URLs to the www equivalent. For example...</p>
<blockquote>
<p><a href="https://example.com" rel="nofollow noreferrer">https://example.com</a>  -&gt;  <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a><br />
<a href="https://example.com/about" rel="nofollow noreferrer">https://example.com/about</a>  -&gt;  <a href="http://www.example.com/about" rel="nofollow noreferrer">http://www.example.com/about</a></p>
</blockquote>
<p>So this is what I've added to the annotations (I'll carry on using <code>example.com</code> as the URL host)...</p>
<pre><code>annotations:
  ...
  nginx.ingress.kubernetes.io/from-to-www-redirect: 'true'
  nginx.ingress.kubernetes.io/configuration-snippet: |
    if ($host = 'example.com' ) {
      rewrite ^ https://www.$request_uri permanent;
    }
</code></pre>
<p>However, if I now navigate to <code>https://example.com</code> I get redirected to <code>https://www.</code>.</p>
<p><a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri" rel="nofollow noreferrer">The docs</a> say <code>$request_uri</code> contains ...</p>
<blockquote>
<p>full original request URI (with arguments)</p>
</blockquote>
<p>... so why is <code>$request_uri</code> apparently empty in my case?</p>

awj
<p><code>$request_uri</code> will contain the path with arguments. To get your desired redirect you probably want to use <code>$host$request_uri</code>.
For some examples of usage of <code>$request_uri</code> you can search it on <a href="https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/" rel="nofollow noreferrer">https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/</a> to see examples or <a href="https://www.webhosting24.com/understanding-nginx-request_uri/" rel="nofollow noreferrer">https://www.webhosting24.com/understanding-nginx-request_uri/</a> also explains it quite well.</p>
