Forum Moderators: phranque
How can I restrict access to pdfs so they can only be accessed through a page on my site?
I'd like to redirect offsite incoming links to the nav page for the pdfs.
They are all in one directory together (about 30 files).
Thanks.
Warning: I have not tested this. There may be errors in logic or typos, but it should give you the idea.
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://(www\.)?mysite.com(/)?.*$ [NC]
RewriteRule .*\.pdf$ [mysite.com...] [R]
These lines, in order mean
1. RewriteEngine On
Turn on the rewrite engine (go figure)
2. RewriteCond %{HTTP_REFERER}!^$
If there's is a referrer (that is referrer is *not* nothing). This means if the user has not just typed in a URi or used a bookmark.
3. RewriteCond %{HTTP_REFERER}!^http://(www\.)?mysite.com(/)?.*$ [NC]
If the referring site is *not* mysite.com (or www.mysite.com, or MySite.com - NC makes the condition case insensitive).
4. RewriteRule .*\.pdf$ [mysite.com...] [R]
If one of the above conditions has been met, apply the rule, which says that any pdf should be rewritten as a redirect to the pdf index.
Note, if *you* have domains other than www.site.com and site.com, you can also negate them in the rewrite conditions.