title: How to write a rule for pdf file that exist in a folder
body: <p>How do I write a rule for files that already exist in a folder to point to a different location. In example, for rewrite any request to a pdf file in the <code>wp-content/uploads/</code> folder to a page <code>download/?q=$1</code>. </p>
  
  <p>I have seen in SO a <a href="https://stackoverflow.com/a/11776591/714967">related question for moved pdf files</a> with the following </p>
  
  <pre><code># checks that the request is for a file that *does not* exist
  RewriteCond %{REQUEST_FILENAME} !-f  
  # make sure we've not already redirected to the uploads directory 
  # (in case it doesn't exist in there either)
  RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
  # the rule matches against either pdf, zip or xls.
  RewriteRule ([^/]*\.(pdf|zip|xls))$ /wp-content/uploads/$1 [NC,L,R=301]
  </code></pre>
  
  <p>But I want the opposite effect. Make the redirection for my existing files and keep a 404 not found for non-existing files.</p>
  
  <p>I have tried different approach without no luck. Wondering if the default <code>.htaccess</code> file of WordPress within the root folder if interfering with this one. How to rewrite all requests of a pdf to a custom page?</p>
  
  <p>EDIT:</p>
  
  <p>The <code>.htaccess</code> file is located in the <code>wp-content/uploads</code>folder. </p>
  
  <p>Regarding the WordPress Rewrite API I already have the following function in a custom plugin:</p>
  
  <pre><code>function add_rewrite_rules($aRules) {
      $new_rules = array(
      'exams/([^/]+)/([^/]+)/?$' =&gt; 'index.php?pagename=exams&amp;type=$matches[1]&amp;city=$matches[2]',
      'exams/([^/]+)/([^/]+)/subject/([^/]+)/?$' =&gt; 'index.php?pagename=exams&amp;type=$matches[1]&amp;city=$matches[2]&amp;subject=$matches[3]'
          );
  
      return $aRules + $new_rules;
  }
  </code></pre>
  
  <p>From my understanding, these functions allows us to specify additional rewrite rules for WordPress as described in the <a href="https://codex.wordpress.org/Rewrite_API/add_rewrite_rule" rel="nofollow noreferrer">Rewrite API/add rewrite rule</a> documentation. So, It should not affect the content of the rules specified in the <code>.htaccess</code> file.</p>