3

In a website builded with Joomla!, I would redirect an external pdf request to a page where the pdf is embedded using iframe or object element.

I tried to do it via htaccess but it doesn't work. It seems that HTTP_REFERER doesn't work well. It seems that the request of embedded pdf is treated as an external request.

When I request mywebsite.com/pdf/filename.pdf, I get redirected to mywebsite.com/filename but also the embedded pdf itself is redirected to mywebsite.com/filename in a infinite loop!

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^mywebsite\.com [NC]
RewriteRule ^pdf/\filename\.pdf$ http://website.com/filename [R=300,L]

Here is the html code of the embedded pdf:

<object data="/pdf/filename.pdf" type="application/pdf">
    <p>This browser does not support PDFs.</p>
</object>

Is there anybody who can help me to understand what's the catch?

How can I redirect an external request of pdf to a page where is the embedded pdf?

Thanks!

Jegger
  • 172
  • 2
  • 13
  • Why are you using 300 (Multiple choices) status instead of 301 (Permanent Redirect) – Amit Verma Apr 10 '17 at 15:12
  • Thank you, unfortunately R=301 wasn't enough to solve the issue. I used R=300 instead of R=301 because this redirect isn't really a permanent redirect. Please see the discussion below. Thank you again. – Jegger Apr 11 '17 at 15:11

1 Answers1

3

Because HTTP_REFERER include protocol (http://), you can't use ^. Or need to add it.

Try with:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !mywebsite\.com [NC]
RewriteRule ^pdf/filename\.pdf$ http://website.com/filename [NC,R=302,L]

Or:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://mywebsite\.com [NC]
RewriteRule ^pdf/filename\.pdf$ http://website.com/filename [NC,R=302,L]
Croises
  • 18,570
  • 4
  • 30
  • 47
  • Thanks a lot. I solved the issue. However, I'm a little puzzled about the use of R=302. According to [w3.org](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html), R=302 means that "The requested resource **resides temporarily** under a different URI." This redirect isn't really a **temporarily redirect**. Wouldn't R=303 be more effective? – Jegger Apr 11 '17 at 15:07
  • You're welcome, glad it worked out. You have the choice, the choice is certainly more linked to the SEO than to another problem. It all depends on whether you want to index the current website or the redirected page. For permanent redirect, I'll use 301 – Croises Apr 11 '17 at 16:28
  • You're right, it's a SEO problem. However, which code would you use? My website is like a scholar journal with PDFs (but I think that the redirect of direct request of PDFs to a page that contains the PDF embedded it would always a best practice in many cases). – Jegger Apr 11 '17 at 17:13
  • 301 doesn't work because it is permanent and so it is cached. Using 301, when I require a pdf directly, I redirected to the html page where it is embedded, but here the pdf it isn't shown (because the embedded element is redirected again). – Jegger Apr 11 '17 at 17:28
  • You are right. In this case it is certainly necessary to stay at 302 or 303.I do not have any idea, however, that Google appreciates one more than the other ... – Croises Apr 11 '17 at 18:18
  • I found to be helpful these discussions: http://stackoverflow.com/questions/4540732/http-302-303-or-307-for-tracking-image-url http://stackoverflow.com/questions/14024855/http-303-seeother-get-works-post-fails Finally I opted for 302. – Jegger Apr 11 '17 at 20:13