title: Nginx location match regex for special characters and encoded url characters
body: <p>I've been trying so many things today and I am just not winning. I have one file in my site which got created by accident with a special character in it. As a result Googlebot has stopped crawling for 3 weeks now and Webmaster tools / Search console keeps notifying me and wanting to retest the url.</p>
  
  <p>All I want to achieve is to configure Nginx to match the following requests and redirect them to the correct location but regex has me stumped on this one.</p>
  
  <p>The unencoded URL string is:</p>
  
  <p><code>/historical-rainfall-trends-south-africa-1921â€“2015.pdf</code></p>
  
  <p>The encoded URL string is:</p>
  
  <p><code>/historical-rainfall-trends-south-africa-1921%C3%A2%E2%82%AC%E2%80%9C2015.pdf</code></p>
  
  <p>How can I get a location match for these?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Still losing my mind, nothing I have tried is working. 
  I get a match with this regex here - <a href="https://regex101.com/r/3Lk2zr/3" rel="nofollow noreferrer">https://regex101.com/r/3Lk2zr/3</a> </p>
  
  <p>but then using this </p>
  
  <p><code>location ~ /.*[^\x00-\x7F]+.* {
          return 444;
          }
  </code> </p>
  
  <p>still gives me a 404 and not a 444</p>
  
  <p>Likewise I get a match with this - <a href="https://regex101.com/r/80KWJ8/1" rel="nofollow noreferrer">https://regex101.com/r/80KWJ8/1</a>
  But then</p>
  
  <p><code>location ~ /.*([^?]*)\%(.*)$ {
          return 444;
          }</code></p>
  
  <p>Gives 404 and not 444  </p>
  
  <p>Also tried this but still no work. Sourced from: <a href="https://serverfault.com/questions/656096/rewriting-ascii-percent-encoded-locations-to-their-utf-8-encoded-equivalent">https://serverfault.com/questions/656096/rewriting-ascii-percent-encoded-locations-to-their-utf-8-encoded-equivalent</a></p>
  
  <p><code>location ~* (*UTF8).*([^?]*)\%(.*)$ {
  return 444;
  }
  </code></p>
  
  <p><code>location ~* (*UTF8).*[^\x00-\x7F]+.* {
  return 444;
  }
  </code></p>
  
  <p><strong>Temporary Solution</strong></p>
  
  <p>Thanks to @funilrys and also this <a href="https://stackoverflow.com/questions/17221408/how-do-i-redirect-all-requests-that-contains-a-certain-string-to-404-in-nginx">How do I redirect all requests that contains a certain string to 404 in nginx?</a></p>
  
  <p>This works now 100%</p>
  
  <p><code>location /resources {
      expires 3h; 
      add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=10800';
          location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
          expires 3h;
          add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=10800';
          }
          location ~*  \.(pdf)$ {
          expires 30d;
          add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=2592000';
          if ($request_uri ~ .*%.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
          if ($request_uri ~ .*[^\x00-\x7F]+.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
          }
  </code></p>