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?
Asked
Active
Viewed 844 times
1
-
Possible duplicate of [Nginx no-www to www and www to no-www](http://stackoverflow.com/q/7947030/1544337) (works the same, just the other way around) or [Nginx rewrite non-www prefixed domain to www-prefixed domain](http://stackoverflow.com/q/1629231/1544337) – Jun 15 '13 at 21:00
2 Answers
0
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).

Déjà vu
- 28,223
- 6
- 72
- 100
-
Thanks for the reply! I am listening on my IP since all domains are an alias for it. The CMS does all the switching.. Im going to try: http://stackoverflow.com/a/1639549/447516 and post my results, it seems to logically be what I've been searching for. We'll see if it works. – Ryan Thompson Jun 14 '13 at 20:22
-
1The second answer seems like what you want "http://stackoverflow.com/a/1691116/2149092", but don't use the rewrite method, use the 301 return as specified in the third answer "http://stackoverflow.com/a/15517045/2149092" because it's less processing for nginx since it doesn't involve regex. – Mohammad AbuShady Jun 14 '13 at 21:17
0
This is the one that got me going:
https://stackoverflow.com/a/3766957/447516
if ($host ~* ^[^.]+\.[^.]+$) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}

Community
- 1
- 1

Ryan Thompson
- 458
- 5
- 15