I have a problem I'm having trouble figuring out and was not able to find a similar problem here at the forum.

I want to be able to redirect a direct request to a pdf file from a search engine, e.g. Google to a html file with a link to the PDF file, in this way I can save some bandwidth from people directly downloading something they do not want to read. AS my pdf-files are on average 10 MB, this can add up.

Please notice that the directory structure is not fixed, the files can be in different folders at different levels. That is why I only want to look at the filename at the end.

Also, I also would like to test wheter the extension is a pdf before a rewrite, as this avoids the server carrying out unneccesary rewrites.

I also want to enable Google and other search engines to index the PDF directly, so I do not want the html page to be served to the search engine.

I think I got the Search engine part correct (Google in the example below, more to be added), but I am not ale to remove the complete filename, only the PDF extension, see below. Practically anything I try gives a server error.

I've been working on the problem and I think I came up with a reasonable solution, see below. The only thing I can't figure out yet is how to test whether the file is a PDF before the rewrite.

I am also open to suggestions on how to do it in another way, especially if it can improve server performance. Also, I am interested in learning a little bit more by seeing other examples. There is probably a more elegant way than this...

No, in this case I know the file exists, I just need to know if the request is for a PDF-file or e.g. a .html file. In case it is a .html file, I do not need to rewrite, in case it is a PDF, I need to go and check who requested it and where from.

You probably don't have a richardk.pdf file, though, so this would not match and a 404 header would be sent.

I just need to know if the request is for a PDF-file or e.g. a .html file. In case it is a .html file, I do not need to rewrite, in case it is a PDF, I need to go and check who requested it and where from.

means that it will also match nothing (equivalent to ^$). In this case it allows for an empty Referer header because some browser's and firewalls do not send it and yu would be denying access by these users. The problem is that this also allows direct requests (you can type the URL into your browser).

means that it will also match nothing (equivalent to ^$). In this case it allows for an empty Referer header because some browser's and firewalls do not send it and yu would be denying access by these users. The problem is that this also allows direct requests (you can type the URL into your browser).

I had not thought about that. However, I want also to redirect the direct request without header to the .html file, so I need to add this rule too: