-1

I have a redirect rule that redirects all PDFs, Word, Excel and Power point documents to a page.

RewriteEngine on
RewriteRule ^(.*).(pdf|doc|docm|docx|dot|dotm|dotx|odt|csv|dbf|dif|xls|xlsb|xlsm|xlsx|xlt|xltm|xltx|xlw|pot|potm|potx|ppa|ppam|pps|ppsm|ppsx|ppt|pptm|pptx)$ http://myurl.ca/file-download-tracker/?file=$1.$2 [R,L]

On that page, I have some code that pushes data about this file to Google Analytics, then redirects back to the file the user was originally trying to access.

I'm trying to write a condition for this rule that will only redirect these files if the user didn't come from the http://myurl.ca/file-download-tracker/?file=myfile.pdf url but having no luck. How could I write this rule?

Here's the closest I think I've gotten with this condition:

RewriteCond %{HTTP_REFERER} !^http://myurl.ca/file-download-tracker/.*
Bash
  • 1,512
  • 2
  • 11
  • 12

1 Answers1

0

I figured out a condition that works, it's able to check that the referer is coming from a page of "file-download-tracker".

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^[^?]+\?([^&]*&)*file-download-tracker(.*)file=(.*)(&.*)?$
RewriteRule ^(.*).(pdf|doc|docm|docx|dot|dotm|dotx|odt|csv|dbf|dif|xls|xlsb|xlsm|xlsx|xlt|xltm|xltx|xlw|pot|potm|potx|ppa|ppam|pps|ppsm|ppsx|ppt|pptm|pptx)$ http://my-url.ca/file-download-tracker/?file=$1.$2 [L,NC,R=302,E=nocache:1]

Now I'm fighting against the browser cache. After redirecting to this page, the browser caches that redirect so when I try to go back to the original file that the user clicked to, the browser takes me back to my redirect page. So it's stuck in a loop right now...

Bash
  • 1,512
  • 2
  • 11
  • 12