Search code examples
http-redirectnginxno-www

NGINX redirect to www in multi-domain environment


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?


Solution

  • This is the one that got me going:

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

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