title: .htaccess redirect if folder accessed directly
body: <p><strong>Folder Structure:</strong></p>
  
  <pre><code>myproject/
   documents/
     1/
      sample.pdf
  </code></pre>
  
  <p>I am allowing user to view the <code>.pdf</code> file but do not allow to view it when access directly through url. If someone access it using directly using below url, it redirects to access denied page.</p>
  
  <pre><code>http://localhost/myproject/documents/1/sample.pdf
  </code></pre>
  
  <p>The code used to achieve this is</p>
  
  <pre><code>RewriteEngine on 
  RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
  RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost .*$ [NC] 
  RewriteRule \.(pdf)$ denied.php [L]
  </code></pre>
  
  <p>Upon further testing, I found that although direct file access is not allowed, but user can still get to know the file upload path and can navigate to docuemnt folder.</p>
  
  <pre><code>http://localhost/myproject/document/
  </code></pre>
  
  <p>which list down all the files uploaded.</p>
  
  <p>How can I restrict user, if <code>document</code> folder OR sub-folders under <code>document</code> folder are accessed directly using URL. </p>
  
  <p>The files inside the folder will be shown regardless of folder access as its happening now.</p>