• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Redirecting all subdomains to www in nginx with virtual hosting

    June 17th, 2011
    tech, nginx  [html]
    If you look for how to redirect to 'www.example.com', you see people writing something where you have to know the hostname, or you have to know all subdomains. What if we need to be much more general, because this server hosts multiple domains, someone might come in on arbitrary subdomain, and we want all requests to go to 'www.REQUESTED_DOMAIN.com'? I came up with the following (it goes in the 'server' block'):
            server_name   ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
    
            if ($subdomains != www.) {
              rewrite  ^/(.*)$  http://www.$domain/$1  permanent;
            }
      
    This will do:
    • www.example.com: no change
    • example.com: www.example.com
    • mail.example.com -> www.example.com
    • mail.example2.com -> www.example2.com
    • foo.bar.baz.example.com -> www.examplecom
    Note that it assumes only one level of tld. So if your site is '.co.uk' or something, it will redirect all requests to 'www.co.uk', which is unlikely to be what you want.

    Recent posts on blogs I like:

    Learning Worst Industry Practices

    If I have a bad idea and you have a bad idea and we exchange them, we now have two bad ideas. But more than that. If I have a bad idea and you have a good idea and we exchange them, we should both land on your good idea – but that requires both […]

    via Pedestrian Observations September 20, 2020

    Collections: Iron, How Did They Make It? Part I, Mining

    This week we are starting a four-part look at pre-modern iron and steel production. As with our series on farming, we are going to follow the train of iron production from the mine to a finished object, be that a tool, a piece of armor, a simple nail, a w…

    via A Collection of Unmitigated Pedantry September 18, 2020

    Learning Game

    I came up with this game. In the game one person thinks of something and then gives the other person a clue. And the other person writes a guess down on a blackboard or a piece of paper. Or really anything you have that's laying around that's av…

    via Lily Wise's Blog Posts September 17, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact