Accessing/downloading non existent .pdf files

on my website I offer users to directly download several .pdf files, the code being:
—a href=“/myfile.pdf” download----a

myfile.pdf gets downloaded all right, only if it exists.
If it does not exist, what is downloaded is the 404.shtml file located in the public_html directory of the website.
Usually the 404.shtml file pops up when something in the URL is not found; but in this case I guess the “download” part of —a href=“/myfile.pdf” download— messes up the expected procedure. So the user instead of the 404 warning gets the 404 file downloaded.
Any idea how to have the “download” part of the snippet be triggered only if the file is available?

Otherwise I guess I’ll have to drop the “download” word, get the 404 warning if the file does not exists, or, if it exists, show the .pdf and let the user manually download the files. Something I’d like to avoid since some files are pretty big and before starting downloading them one has to wait for the full file to be loaded.
Suggestions welcome. Thanks.

Edited: sorry I had to remove the expected glyph at the beginning and end of the code and replace it with “----”.

I think you’ll have to muddle around with your web server’s error redirect settings. You’ll want to tell it to not redirect on that specific file or directory, while preserving the 404 redirect on the rest of the site.

Request the headers with a HEAD request and don’t attempt to download the file if the HTTP status is 404. This way you won’t have to adjust your server configuration, so your already working systems don’t need to change.

1 Like

@ Eric_Williams and @Tim_Parnell
Thank you for pointing out that there is a way out.
Meantime I’m considering a simpler solition: zipping each .pdf book I reduce, for instance, 45mega to 35mega, and if the zipped file does not exists the user gets the 404 warning.
Yet I wont deny that the “simpler” solution sounds like the “nondum matura est” said by the fox not able to jump up to the ripe grapes.

How do you create the href links? Maybe there is a way to make sure that the html code always points to an existing file?

I’d say in the usual way, as I showed above. Only that I add " download" after the URL in order to have the file downloaded instead of having it shown. As in:

Get the <a href=“/myfile.pdf” download>file<a> in order to be happy.

But as I said, I’m considering the option of dropping the “download” tag and zipping the files.

This is not a valid href.

@Beatrix Willius
“This is not a valid href”

Yes, in struggling in order to have the “more-than” etc. glyghs visible in the post, the closing <a> remained orfaned; it should have been </a>.

Or are you referring to something else?

My question is more like
Why the file to download is not there?

If you have files on a server and you are creating the HTML in some way then how a link to a file that is not there is created with the download attribute?

Because when I have a new book ready to be to uploaded, it may happen that I upload the .html file (containing the new link) but forget to upload the .pdf file thinking I’ve already uploaded it.
You may now ask why on earth should I forget to upload the file. The answer is that age plays strange tricks to memory.
Next question: am I over 90? No, I’m much younger, nearer to 80 than to 70.

1 Like

Perhaps, then, a better solution would be to periodically run a dead link checker on your site. That would catch any missing resources, and is generally a good idea for any site.

That is something I have always done and still do, specifically for the couple of pages containing links to files intended to be accessed or downloaded.