• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Redirecting all subdomains to www in nginx with virtual hosting

    June 17th, 2011
    nginx, tech
    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:

    A Big Problem With The Going To Bed Book

    One day my dad was reading this book called the "Going to Bed Book" to my sister Nora. The book is basically about a bunch of animals who are getting ready for bed on a boat. They go down the stairs, take a bath, hang their towels on the wall, find…

    via Lily Wise's Blog Posts September 18, 2023

    Investing in boundaries with young kids

    Putting in some work to get the behavior you want The post Investing in boundaries with young kids appeared first on Otherwise.

    via Otherwise August 15, 2023

    Self-driving car bets

    This month I lost a bunch of bets. Back in early 2016 I bet at even odds that self-driving ride sharing would be available in 10 US cities by July 2023. Then I made similar bets a dozen times because everyone disagreed with me. The first deployment to pot…

    via The sideways view July 29, 2023

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact