Nginx: Forward all Subdomains
        Posted  
        
            by mgutt
        on Server Fault
        
        See other posts from Server Fault
        
            or by mgutt
        
        
        
        Published on 2010-05-25T16:01:30Z
        Indexed on 
            2010/05/25
            16:03 UTC
        
        
        Read the original article
        Hit count: 330
        
My code doesn't work with second level tld's like domain.co.uk
The strainge thing is, that it works in php with preg_match() ^^
Here is my conf:
  # add www.
  if ($host ~ ^(?!www)) {
   rewrite ^/(.*)$ http://www.$host/$1 permanent;
  }
  # remove subdomain
  if ($host ~ "^www\.(.*)\.(.*\.([a-z]{2,4}|[a-z]{2}\.[a-z]{2}))") {
   set $host_without_sub $2;
   rewrite ^/(.*)$ http://www.$host_without_sub/$1 permanent;
  }
        © Server Fault or respective owner