How to rewrite PDF URLs to an intermediate tracking page before displaying
===

I'm trying to rewrite PDFs URLs to an intermediate html page using a parameter 'ref' set to the original PDF path. This intermediate page is an analytic tool used to track a PDF view. It uses JavaScript to record a view and then redirect back to the actual PDF source URL.

http://www.mydomain.com/dir/some-pdf.pdf should rewrite to: http://www.mydomain.com/elqNow/elqRedir.htm?ref=http(s)://www.mydomain.com/dir/some-pdf.pdf

Basically the rule needs to check for /elqNow/elqRedir.htm in %{REQUEST_URI}, %{HTTP_REFERER} and the 'ref' parameter needs to be the full path e.g http(s)*://%{HTTP_HOST}/PDF_URL (Can't figure out how to add http or https without hard coding it.) 

Here's what I have so far, which creates a nasty loop:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !/elqNow/elqRedir\.htm$
    RewriteRule ^(.+\.pdf|PDF)$ elqNow/elqRedir.htm?ref=http://%{HTTP_HOST}/$1 [R=301, L] 

Appreciate the help!


