title: How to redirect in nginx if request URI does not contain certain words
body: <p>I'm using nginx to serve static news-like pages.
  On the top-level there is
  <code>https://example.com/en/news/</code> with an overview of the articles.
  Individual items have a URL similar to this: <code>https://example.com/en/news/some-article</code></p>
  
  <p>All URLs contain the language, i.e. <code>/en/</code> or <code>/de/</code>.
  I would like to create a rule that redirects requests that <strong>don't</strong> contain the language to the correct URL (the language is mapped based on IP an available via <code>$lang</code>).</p>
  
  <p>The following should work (<code>en</code> example):</p>
  
  <pre><code>/news/             --- redirect ---&gt; /en/news/
  /news/some-article --- redirect ---&gt; /en/news/some-article
  </code></pre>
  
  <p>My attempts looked something like this</p>
  
  <pre><code>location ~* /news/.*$ {
      if ($request_uri !~* /(de|en)/$) {
          return 302 https://example.com/$lang/$request_uri;
      }
  }
  </code></pre>
  
  <p>So far this resulted in infinite redirects.</p>