title: Nginx 404 to 301 redirect based on URI
body: <p>I am trying to create a multiple 404 redirects in WordPress, and I am currently using this:</p>
  
  <pre><code>error_page 404 @foobar;
  
  location @foobar {
      rewrite  .* / permanent;
  }
  </code></pre>
  
  <p>I know this is meant for a redirect to the homepage, but <strong><em>this method is not working at all</em></strong>, for it defaults to the regular WordPress 404.php file that is included with the theme.  That is fine, but I need to get the redirect working via Nginx instead of WP because I need to create different redirects once a page has been declared a 404.</p>
  
  <p>Example:
  Say I have about 10 categories on my WP Site.
  I have posts that come and go, and once a post is gone, I would like to redirect that post (because each post in a certain category has a unique identifier) to the base category.</p>
  
  <p>Example: redirect non existent post</p>
  
  <pre><code>/i-love-soup-cause-it-tastes-so-good-campbellssoup ---&gt; /category/campbellssoup
  </code></pre>
  
  <p>or</p>
  
  <p><code>/toystory-3-is-a-great-movie-coolmovies ---&gt; /category/coolmovies</code></p>
  
  <p>Basically I need to create multiple redirects to multiple locations ONLY AFTER the URI has been established as a 404.  Any ideas?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok, using </p>
  
  <pre><code>fastcgi_intercept_errors on;
  </code></pre>
  
  <p>I managed to take the virtual errors out of wordpress.  Now I have the redirect to <code>@foobar</code> working, but I am unable to get the uri's to transfer to the <code>@foobar</code> location block.  I am guessing it is something to do with my try_files in my / location block.</p>
  
  <pre><code>location / {
      #try_files $uri $uri/ /index.php?q=$request_uri;
      try_files $uri $uri/ /index.php?$args;
  }
  </code></pre>