Link to home
Start Free TrialLog in
Avatar of ChampagneGal
ChampagneGalFlag for United States of America

asked on

htaccess file to restrict download of pdf files unless they are a registered user

I'm new to this. I want to make sure that my pdf files cannot be downloaded if someone just types that path to the pdf file in the browser.  I plan on restricting the link to the pdf files on my webpages in my php code.  

It sounds like I need to use an .htaccess file but I'm not sure how to write it.  Can anyone help me???

Thanks in advance...
Avatar of dsmile
dsmile
Flag of Viet Nam image

Try this: any attempt to get .pdf files directly will be redirected to somesite you want.
RewriteEngine On
RewriteCond %{REQUEST_URI} \.pdf$ [NC]
RewriteRule (.*) http://somesite [L,R]

Open in new window

If you just want to show a 403 page, to any request on pdf file, then you can use this instead


<FilesMatch "\.(pdf)$">
order deny,allow
deny from all
</FilesMatch>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of benschwartz
benschwartz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChampagneGal

ASKER

Thank you so much.  I did not know that was possible so I'm looking forward to trying this.
If I put my video files outside of the root will that keep people from being able to download them via Real Player?
Avatar of benschwartz
benschwartz

yeah, same principal - just proxy it through with php's file_get_contents()

If you run into trouble with the browser recognizing the file type, you may have to apply a mime type with the header function. You can also force the file to download as it's original name (and any number of other things) with the header function:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');

If you like, also look at "readfile()" as a file_get_contents() alternative.
Thanks, I'll try it tomorrow.  Happy Thanksgiving  :-)