• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • Redirecting all subdomains to www in nginx with virtual hosting

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

    More on the Deutschlandtakt

    The Deutschlandtakt plans are out now. They cover investment through 2040, but even beforehand, there’s a plan for something like a national integrated timetable by 2030, with trains connecting the major cities every 30 minutes rather than hourly. But the…

    via Pedestrian Observations July 1, 2020

    How do cars fare in crash tests they're not specifically optimized for?

    Any time you have a benchmark that gets taken seriously, some people will start gaming the benchmark. Some famous examples in computing are the CPU benchmark specfp and video game benchmarks. With specfp, Sun managed to increase its score on 179.art (a su…

    via Posts on Dan Luu June 30, 2020

    Quick note on the name of this blog

    When I was 21 a friend introduced me to a volume of poems by the 14th-century Persian poet Hafiz, translated by Daniel Ladinsky. I loved them, and eventually named this blog for one of my favorite ones. At some point I read more and found that Ladinsky’s …

    via The whole sky June 21, 2020

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact