title: Nginx Domain Name Redirects with Maps
body: <p>I have a long list of older domain names that need to be redirected to a path on a newer domain - ie. foo.com, bar.com should be redirected to foobar.com/one and foobar.com/two (ie. I can't regex the new URL out of the old one)</p>
  
  <p>My attempt:</p>
  
  <pre><code>map $uri $newdomain {
      include redirects/domains.conf;
  }
  
  server {
      listen 80;
  
      server_name .......; # this handles all of the domains
  
      if ($newdomain) {
          return 301 $newdomain;
      }
  
      # the rest of the config - handling the correct new domain request
  }
  
  # in redirects/domains.conf
  foo.com    foobar.com/one;
  bar.com    foobar.com/two;
  foo.com/bar    foobar.com/three;
  </code></pre>
  
  <p>Nginx reloads successfully, but it doesn't apply the redirects.</p>
  
  <p>All of the examples of maps I've seen only have it handling paths - i.e. /page-one to /page-two</p>
  
  <p>What is the best approach for this?</p>