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...
https://example.com -> https://www.example.com
https://example.com/about -> http://www.example.com/about
So this is what I've added to the annotations (I'll carry on using example.com
as the URL host)...
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;
}
However, if I now navigate to https://example.com
I get redirected to https://www.
.
The docs say $request_uri
contains ...
full original request URI (with arguments)
... so why is $request_uri
apparently empty in my case?
$request_uri
will contain the path with arguments. To get your desired redirect you probably want to use $host$request_uri
.
For some examples of usage of $request_uri
you can search it on https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ to see examples or https://www.webhosting24.com/understanding-nginx-request_uri/ also explains it quite well.