如何使用url限制正在查看或下载的文件? 我希望使用PHP脚本下载相同的文件

I want to restrict a pdf file on my server from being viewed or downloaded. for example, when somebody types below url in browser and press enter.

http://example.com/pdf/sample-pdf1.pdf

the file should not be viewed or downloaded. instead, it should get redirected somewhere else or should display some error message

But, when i download the same file using php or html code, it should get downloaded

for example when i use below code in a php file, it should get downloaded.

[a href="http://example.com/pdf/sample-pdf1.pdf" download]Download[/a]

Insert .htaccess file inside pdf directory with content: "Deny from all" - That will not allow access that directory and files inside it via URL.