• 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:

    How much time and money does an additional child take?

    Some things scale, others don't. The post How much time and money does an additional child take? appeared first on Otherwise.

    via Otherwise March 19, 2023

    What does Bing Chat tell us about AI risk?

    Early signs of catastrophic risk? Yes and no.

    via Cold Takes February 28, 2023

    Why Neighborhoods Should Have Speed Bumps

    I have several reasons I think why neighborhoods should have speed bumps. First, speed bumps are very useful to stop cars from hitting people in the streets. Second, when construction workers installed speed bumps on the street in front of our house it was v…

    via Lily Wise's Blog Posts February 27, 2023

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact