title: nginx rewrite rules from main domain to subdomain
body: <p>In my nginx domain.cong , I wrote the following rewrite rules ... 
  when a request hits the main domain ( with or without www) it shoudl redirect ot the blog subdomain , but it seems to be wrong ... </p>
  
  <pre><code>server {
  ....
  ##### Rewrite rules for domain.tld =&gt; www.domain.tld #####
  if ($host ~* ^([^.]+\.[^.]+)$) {
      set $host_without_www $1;
      rewrite ^(.*) $scheme://www.$host_without_www$1 permanent;
  }
  
  ##### Rewrite rules for www.domain.tld =&gt; subdomain.domain.tld #####
  if ($host ~* 'www\.[^.]+\.[^.]+$')  {
      set $host_without_www $1.$2;
      rewrite ^(.*) $scheme://subdomain.$host_without_www$1 permanent;
  } 
  ...
  } 
  </code></pre>
  
  <p>The first rule is correct:<br>
  <code>domain.tld =&gt; www.domain.tld</code><br>
  but not the second one giving only<br>
  <code>www.domain.tld =&gt;  subdomain.</code><br>
  should be<br>
  <code>www.domain.tld =&gt;  subdomain.domain.tld</code></p>