Forum Moderators: phranque

Message Too Old, No Replies

embed pdf and mod rewrite

         

daniel31

5:44 pm on Oct 8, 2014 (gmt 0)

10+ Year Member



I would like to capture accesses to my pdf files and generate an html response with the pdf embeded in an object tag or a frame


I tried with `mod_rewrite` in my **.htaccess**
 
RewriteEngine on
RewriteRule \.pdf$ pdf.php [L]



and **pdf.php** something like

$uri=$_SERVER["REQUEST_URI"];
print("<center><object data='$uri' type=application/pdf width=728px height=90%>");


it captures the pdf access but displays an empty gray frame without the pdf
I suspect that the rewrite engine tries to apply the rule again to the
embeded pdf file ?

lucy24

6:39 pm on Oct 8, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suspect that the rewrite engine tries to apply the rule again to the embeded pdf file

Yes, it looks as if you're setting up an infinite loop. Been there. Done that. If so, you need a preceding RewriteCond that says "only do this if the referer is NOT 'pdf.php'". And then you've still got a problem with browsers that don't send a referer.

What do your logs (both access and error) say?

daniel31

9:24 pm on Oct 8, 2014 (gmt 0)

10+ Year Member



thank you for your answer.
I confirm that I enter an infinite loop. Unfortunatelly there is no referer in the logs.
btw I found a work around solution to distinguish between direct access and access from the embed object. It consist in adding a query string to the pdf like this <object data='$uri?escape=1' ....
and the access log:

127.0.0.1 - - [08/Oct/2014:23:23:01 +0200] "GET /test.pdf HTTP/1.1" 200 175
127.0.0.1 - - [08/Oct/2014:23:23:01 +0200] "GET /test.pdf?escape=1 HTTP/1.1" 200 193
Now I need directive to escape pdf.php when
the query string contains "escape=1"
how to proceed ? I am not familiar of RewriteCond
and RewriteRule

lucy24

12:41 am on Oct 9, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah. I had a nasty feeling that a php request might not behave exactly like an external request. What you need, instead, is a condition looking at the query string. In fact, if the only query string is the one appended by the php, you need not consider the exact text, just the fact that one exists. It would be a negative condition:

RewriteCond %{QUERY_STRING} !.


meaning "if a query string exists" (or, negatively, "doesn't exist"). The . means "any character".

daniel31

3:02 pm on Oct 9, 2014 (gmt 0)

10+ Year Member



I found a solution:


RewriteCond %{QUERY_STRING} !escape=1 [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteRule . index.php [L]


it does what i want: when I access first my.pdf it goes to index.php


$uri=$_SERVER["REQUEST_URI"];
if (endsWith($uri,".pdf")) {
$out="<center><object data='$uri?escape=1' type=application/pdf width=728px height=90%>";
print($out);
return;
}

then the query strind escape=1 makes the rule index.php skipped

lucy24

5:36 pm on Oct 9, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteRule . index.php [L]

Huh?

Now you seem to be writing an entirely different rule. Whenever possible, constrain the body of the rule so the server doesn't need to evaluate conditions at all on requests that won't apply. So the rule should start out with

RewriteRule ^filename\.pdf

phranque

8:31 am on Oct 13, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, daniel31!


RewriteCond %{QUERY_STRING} !escape=1 [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteRule . index.php [L]


it does what i want: when I access first my.pdf it goes to index.php


i think you want:
RewriteCond %{QUERY_STRING} !escape=1 [NC]
RewriteRule ^my\.pdf$ index.php [L]

daniel31

12:40 pm on Oct 13, 2014 (gmt 0)

10+ Year Member



Thanks

I found a solution

In fact a final objective was to decorate existing html & pdf with ads (adsense)

first the .htaccess

RewriteCond %{QUERY_STRING} !noads=1
RewriteCond %{REQUEST_URI} \.(htm|html|pdf)$
RewriteRule . index.php


next index.php:

$root=$_SERVER["DOCUMENT_ROOT"];
$uri=$_SERVER["REQUEST_URI"];
if ((endsWith($uri,".html") ||endsWith($uri,".htm"))){
$f=$root.$uri;
$c = file_get_contents($f);
$c=addads($c);
print($c);
return;
}


else if (endsWith($uri,".pdf")) {
//embed pdf with noads=1 to skip rewrite rules and avoid infinite loop
$c="<object width=800 height=800 data=$uri?noads=1 type=text/html codetype=application/pdf ></object>";
$c=addads($c);
print($c);
return;
}


function addads($content) { // add banners
return "<table><td>left</td>
<td>top<br>$content<br>bottom</td>
<td>right</td></table>";
}


the rewrite conditions make pdf and html to be served by index.php
except if the query-string contains "noads=1"

for embeding pdf I prefer [pdfobject.com ] rather than <object>
with url="$uri?noads=1" to avoid infinite loop


working exemples from my site, for pdf:

  • [yats.com ] pdf
  • [yats.com ]pdf with ?noads=1

    html:

  • [yats.com ]html
  • [yats.com ] html with noads=1
  • lucy24

    5:03 pm on Oct 13, 2014 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    I simply don't see why this part
    RewriteCond %{REQUEST_URI} \.(htm|html|pdf)$
    RewriteRule . index.php

    needs to be in a Condition instead of the body of the rule. The server is being put to extra work on every request.