Question

How can I redirect non-www to www in NGINX when the domain name is not predetermined? All the examples I've found show domain.com or example.com. I need to redirect for any domain. Any pointers?

Was it helpful?

Solution 2

This is the one that got me going:

https://stackoverflow.com/a/3766957/447516

if ($host ~* ^[^.]+\.[^.]+$) {
    rewrite ^(.*)$ http://www.$host$1 permanent;
}

OTHER TIPS

You can specify the default server to catch domains that are not specified in the server_name directives of your configuration.

listen 80 default_server;

will make that server catch non matching domain names (assuming the port is 80 on all interfaces).

This is a clear explanation of this behavior.

(not that before nginx 0.8.21 default (not default_server) has to be specified).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top