title: How to replace characters in an nginx variable string?
body: <p>Is there a way I can replace non alphanumeric characters returned with <code>$request_uri</code> with a space (or a <code>+</code>)?</p>
  
  <p>What I'm trying to do is redirect all 404's in one of my sites to it's search engine, where the query is the <code>uri</code> requested.   So, I have a block in my nginx.conf containing:</p>
  
  <pre><code>error_page 404 = @notfound;
  location @notfound {
      return 301 $scheme://$host/?s=$request_uri;
  }
  </code></pre>
  
  <p>While this does indeed work, the url's it's returning are the actual <code>uri</code>'s complete with <code>-_/</code> characters causing the search to always return 0 results</p>
  
  <p>For instance...  give this url: <code>https://example.com/my-articles</code>, the redirect ends up as this: <code>https://example.com/?s=/my-articles</code></p>
  
  <p>What I would like is to end up (ultimately) like this: <code>https://example.com/?s=my+articles</code>   (tho, the + at the beginning works fine too...  <code>https://example.com/?s=+my+articles</code></p>
  
  <p>I will need to do this without LUA or Perl modules.  So, how can I accomplish this?</p>