'how to redirect a url that does not contain specific text using htaccess?

I have a site that has a some pdf files to download. i want to block direct file download. example: if a user try https://skpselearning.enovic.in/uploads/document/viewer.html?file=lesson9.pdf it should download. but without viewer.html? it will should be block. ie https://skpselearning.enovic.in/uploads/document/lesson9.pdf this url will be block. how to do this?



Solution 1:[1]

(You've not stated how your viewer.html script is downloading/displaying the .pdf files? Simply blocking direct access to the .pdf files could also block access to your script, depending how it is implemented.)

So, all you are really asking (and all that can be answered) is how to block direct access to the .pdf files...

To block (403 Forbidden) HTTP access to all .pdf files on the site:

<Files "*.pdf">
    Require all denied
</Files>

Or, to block only .pdf files in the /uploads/document subdirectory then you can use the following mod_rewrite directives at the top of the .htaccess file:

RewriteEngine On

RewriteRule ^uploads/document/[^/]+\.pdf$ - [F]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1