1

I have a directory structure: httpdocs/documents/folder1 httpdocs/documents/folder2 httpdocs/documents/folder3

etc in httpdocs/document I have a .ht access file:

RewriteEngine on
RewriteRule ^(.*).(PDF)$ fileopen.php

and in this folder I have fileopen.php

This file then opens pdf files in folder1 to folder3 depending on a $_Get request sent by a file request in the httpdocs folder.

I want the htaccess file to limit access to all folders but allow any pdf being requested by fileopen.php to be downloaded and opened. Is this possible cause I can't get it to work.

Cœur
  • 32,421
  • 21
  • 173
  • 232
user1616338
  • 489
  • 4
  • 20

2 Answers2

0

Try something like this:

RewriteEngine on
RewriteRule ^(.*).(PDF)$ fileopen.php?file=$1 [L]
RewriteRule ^(folder1|folder2|folder3) - [L,F]
Jon Lin
  • 135,941
  • 26
  • 200
  • 209
  • @user1616338 it's up to your `fileopen.php` script to [return the proper content type](http://stackoverflow.com/questions/312230/proper-mime-media-type-for-pdf-files) then read and output the pdf file. – Jon Lin Oct 10 '13 at 15:28
0

Though it is possible to block it using mod_rewrite rules. I guess most simple and robust solution is to move folder1, folder2, folder3 outside DOCUMENT_ROOT.

Then you can have httpdocs and a new pdfdir at same level. pfdir can then contain folder1, folder2, folder3.

Something like this:

---> httpdocs/
----------> .htaccess
----------> fileopen.php
----------> documents/
---> pdfdir/
----------> folder1/
-------------------> foo.PDF
----------> folder2/
-------------------> bar.PDF
----------> folder3/
-------------------> baz.PDF

This way pdfdir and its contents are inaccessible from web and your index.php can still access them using fopen etc file functions.

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • I didn't want to use stuff outside the webroot but wanted to try to make htaccess work- however I gave up putting it outside the webroot was pretty straightforward and like you say robust thanks – user1616338 Oct 11 '13 at 10:19
  • You're welcome, keeping those files outside is way more secured than any other solution. – anubhava Oct 11 '13 at 10:20