This is What I have
http://example.com ---> https://www.example.com ---> https://example.com
This is what I am trying to Achieve
http://example.com ---> https://example.com
My current nginx config has the below mentioned line
if ($real_scheme = 'http') { return 301 https://$host$request_uri; }
Where real_scheme is a variable derived from a Map block
I am trying to Achieve the same using a Map, can anyone lemme know the mistake I am making, Below is the updated Config
map $host $nonwwwhost {
~*^www.(.*) $1;
default $host;
}
if ($real_scheme = 'http') { return 301 https://$nonwwwhost$request_uri; }
2
Answers
Finally figured out a way to Achieve this using regex expression
I would say that using
map
for https redirects is discouraged, because the standard practice is allocatingserver
blocks with redirects set up inside them.There are a total of 3 server blocks required to cover redirecting to your desired canonical domain name and port.
Redirections flow can be different, but I suggest following with HSTS redirect requirements.
In a case where your canonical domain is example.com and not http://www.example.com, these would be your server blocks:
This kind of setup ensures, that should a visitor first access http://www.example.com, he is first redirected to https://www.example.com and then https://example.com, ensuring his browser obtains HSTS policy accordingly.