Question
Nginx redirect only root domain but not subdomain to www
Currently I’m using setting below to redirect non-www domain to www domain and it’s working fine:
server {
listen 80;
servername example.com;
return 301 http://www.example.com$requesturi;
}
server {
listen 80;
servername www.example.com;
location / {
proxypass http://www.example.com:8888;
proxyhttpversion 1.1;
proxysetheader Upgrade $httpupgrade;
proxysetheader Connection ‘upgrade’;
proxysetheader Host $host;
proxycachebypass $httpupgrade;
}
}
However, now I would like to allow wildcard subdomain but it seems like all the subdomains were being redirect to www.domain.com. So my question is how can I make it only redirect the root domain to www only and excluding all other subdomain? Thanks.
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×