title: Redirecting wildcard subdomains to a different top-level domain with nginx
body: <p>We have a bunch of wildcard subdomains (_foo.example.com, bar.example.com, etc) that, when accessed via HTTPS should redirect to the equivalent subdomain on our secure domain.</p>
  
  <p>Some examples:</p>
  
  <ul>
  <li><a href="https://foo.example.com" rel="nofollow">https://foo.example.com</a> => <a href="https://foo.secure.com" rel="nofollow">https://foo.secure.com</a></li>
  <li><a href="https://foo.example.com/some/path" rel="nofollow">https://foo.example.com/some/path</a> => <a href="https://bar.secure.com/some/path" rel="nofollow">https://bar.secure.com/some/path</a></li>
  <li><a href="https://bar.example.com" rel="nofollow">https://bar.example.com</a> => <a href="https://bar.secure.com" rel="nofollow">https://bar.secure.com</a></li>
  </ul>
  
  <p>I think this can be accomplished with nginx rewrites but I'm not sure about the syntax.  Here's what I'm attempting:</p>
  
  <pre><code>server {
      listen        443;
      server_name   *.example.com;
  
      rewrite       ^(.*)   https://*.secure.com$1 permanent;
  }
  </code></pre>
  
  <p>This obviously won't work because I'm not capturing the incoming subdomain and using it in the rewrite.</p>