如果直接定位,则限制对文件的访问

I have a basic folder structure like this:

index.php
assets/
  docs/
    document.pdf
includes/
  script.php

I need to be able to block access to the document if someone was to try and get it directly, for example: www.domain.com/assets/docs/document.pdf but allow access if was to be linked from the index.php page.

Would there be a way I can block access like this, or, is there a method of embedding the pdf on the page? I'm open to using PHP or jQuery if it can be done through that as well.

Some of the methods I have tried have been including deny restrictions in the htaccess but then that obviously blocks it altogether.

Thanks in advance.

You can use the Referer header directly in your Apache configuration (an assets/docs/.htaccess file will do):

SetEnvIf Referer "domain\.com" is_local_referer
<FilesMatch "\.pdf$">
    Require env is_local_referer
</FilesMatch>