There are a lot of questions on ServerFault that cover parts of this question, but I'm having a hard time piecing them together. I have a fresh nginx server with a Wildcard SSL cert installed. All that works fine. I'd like to create redirect rules that enforce the following:
1) There must be either www or a subdomain enforced. So, subdomain.domain.com and www.domain.com is fine. domain.com by itself is not, and should automatically add the www.
2) HTTPS must be enforced throughout the site, regardless of subdomain.
In other words:
ttp://domain.com >> ttps://www.domain.com
ttp://subdomain.domain.com >> ttps://subdomain.domain.com
ttps://domain.com >> ttps://www.domain.com
ttps://subdomain.domain.com (this is fine)
Per the Nginx rewrite pitfalls page and other answers on this site, I've been using this config code:
server {
listen 80;
server_name *.domain.com;
return 301 https://$server_name$request_uri;
}
as well as this:
server {
listen 80;
server_name ~^(.*)domain\.com;
return 301 https://$server_name$request_uri;
}
I've also tried using $host instead of $server_name.
All of this redirects the http > https just fine, but it always defaults to https://domain.com, regardless of subdomain or www. Thoughts?