title: How to only redirect urls located in a nginx Map
body: <p>I am in the process of moving my blog from <code>a.com</code> to <code>b.com</code>. Now I want to tell Google /bookmarks that all blogposts (around 100) have been moved to <code>b.com</code>. I only want to redirect the blogposts and nothing else.</p>
  
  <p>After reading about the <a href="http://wiki.nginx.org/HttpMapModule" rel="nofollow">map</a> module in nginx I tried the following:</p>
  
  <pre><code>map_hash_bucket_size 128;
  map $uri $new {
    /post http://b.com/post
    # (repeated for all 100 posts)
  }
  </code></pre>
  
  <p>And when I put the following line inside the <code>server</code> block:</p>
  
  <pre><code>rewrite ^ $new redirect;
  </code></pre>
  
  <p>It will redirect all 100 posts but all the other pages on my domain will error with: <code>302 Found</code>.</p>
  
  <p>Here is my whole server block inside the config:</p>
  
  <pre><code>server {
  
      listen 80;
      server_name b.com;
      root /my/old/path/;
      index index.html;
  
      location = /favicon.ico {
              log_not_found off;
              access_log off;
      }
  
      location = /robots.txt {
              allow all;
              log_not_found off;
              access_log off;
      }
  
      # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
      location ~ /\. {
          deny all;
          access_log off;
          log_not_found off;
      }
  
      location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
             expires max;
             log_not_found off;
      }
  
      # example.com/index gets redirected to example.com/
  
      location ~* ^(.*)/index$ {
          return 301 $scheme://$host$1/;
      }
  
      # example.com/foo/ loads example.com/foo/index.html
  
      location ~* ^(.*)/$ {
          try_files $1/index.html @backend;
      }
  
      # example.com/a.html gets redirected to example.com/a
  
      location ~* \.html$ {
          rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
      }
  
      # anything else not processed by the above rules:
      # * example.com/a will load example.com/a.html
      # * or if that fails, example.com/a/index.html
  
      location / {
          try_files $uri.html $uri/index.html $uri @backend;
      }
  
      # default handler
      # * return error or redirect to base index.html page, etc.
      location @backend {
          try_files /404.html 404;
      }
  
      location ~ \.php$ {
              expires off;
              try_files $uri =404;
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  
              include fastcgi_params;
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_intercept_errors on;
              fastcgi_pass php;
      }
  
      # apply the rewrite map
      rewrite ^ $new redirect;
  }
  </code></pre>
  
  <p>I am thinking the map interferes with the <code>try_files</code> call for location <code>\</code> (which I need).</p>