The solution I came up with is really nice, and will work for all .pdf files on your entire site, without having to change any links. It is a secure and robust solution to my pet peeve. Feel free to add any ideas or improvements using the comment form below.
For me an ideal solution would be to offer me choices, such as giving me the option to:
Because serving PDF files with the above options would make me happy as a visitor, I want to offer those options to visitors on my site. Here is how I do it.
This .htaccess mod_rewrite code goes in your sites root .htaccess file and uses a RewriteCond to make sure that the file exists, it then rewrites the request internally to the /cgi-bin/pdf.php
file while adding the PDF filename as a http GET variable called file
.
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.+).pdf$ /cgi-bin/pdf.php?file=$1 [L,NC,QSA]
This php code is simple yet sophisticated, it simply locates the pdf file requested using the GET variable called file
and then sends the pdf file directly to the clients browser, adding some HTTP headers to make a built-in browser dialog window pop-up giving the client the option to open the pdf in an external program or to save the pdf on the clients computer.
Final Working Example