title: nginx www redirect except other subdomains
body: <p>Probably yet another nginx redirect question... couldn't find the answer, so:</p>
  
  <p>How do I redirect <a href="http://domain.com" rel="nofollow">http://domain.com</a> to <a href="http://www.domain.com" rel="nofollow">http://www.domain.com</a> but do not rewrite any subdomain http://*.domain.com given the cloudfoundry nginx configuration which contains this server definition:</p>
  
  <pre><code>server {
      listen       80;
      server_name  _;
      server_name_in_redirect off;
  }
  </code></pre>
  
  <p>I tried this configuration</p>
  
  <pre><code>server {
       server_name domain.com
       rewrite ^(.*) http://www.domain.com$1 permanent;
  }
  
  server {
      listen       80;
      server_name  _;
      server_name_in_redirect off;
  }
  </code></pre>
  
  <p>but am getting infinite redirects.</p>