We help IT Professionals succeed at work.
Get Started

.htacess: how to check if referrer was site XYZ?

Last Modified: 2012-05-06
I have two websites:
1) mysite-AAA.com
2) mysite-BBB.com

Mysite-AAA.com contains a folder with a number of PDFs that need to be password protected:
--> mysite-AAA.com/pdf_files/

To prevent users from directly linking to the PDFs, i added the .htaccess file below. When the user logs into mysite-AAA.com, a cookie ("fileaccess") is set. If the cookie has not been set, the user is redirected to the "sign in" page.

Now I want to link to these files from mysite-BBB.com. The user won't see the links until they have logged into mysite-BBB.com.

The problem is that I can't check for a cookie because the .htaccess lives on mysite-AAA.com.

I thought I could add a new condition to check to see if the referring site was mysite-BBB.com...but the PDFs are not loading properly when linked to from mysite-BBB.com.

The files DO load properly if the user is logged into mysite-AAA.com.

Any better suggestions for handling this?
# NOTE: This is the .htaccess file in the mysite-AAA.com/pdf_files/ directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !fileaccess= [NC]
RewriteCond %{HTTP_REFERER} !mysite-BBB.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://([a-z0-9\-]+\.)?mysite-BBB.com/.*$ [NC]
RewriteRule .* /index.php?page=sign_in [NC,L,QSA]

Open in new window

Watch Question
Top Expert 2008
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE