This setup is for multiple domain sharing a single CMS platform. It will allow any domain to point to my server ip.
I can use nginx server_name _; to catch all.
But then I would like to 301 redirect to www (appending www. infront) if www is not present. If www is present, it's fine.
How can i achieve this?
I have solved this by using following nginx server configuration.
server {
listen 80 default_server;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
server {
listen 80;
server_name ~^(?<domain>.+)$;
root /home/wwwroot/web;
..
..
}
This will allow any domain and 301 redirect to www.
You can catch the without www and redirect to www with this
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
root /path/to/site;
# Extra conf here.
}