Ubuntu: How to block pdf file in apache2 - Ubuntu16.04



Question:

I have a pdf file under my document root (/var/www/html) name is test.pdf. I want to restricted my pdf file if anyone try in browser like http://example.com/test.pdf, in this case i want pdf not view. I already try by htaccess and restricted that directory but failed.

Can anyone help me to provide any trust solution?


Solution:1

You can add this code in to the to the apache httpd config:

<Files  ~ "\.pdf$">    Order allow,deny    Deny from all  </Files>  

This code will deny access to all the *.pdf files. But the question is why you keep in httpd docs directory files you do not want to expose to the world.

If you want to restrict access to only one file the code in config should be like:

<Files  test.pdf>    Order allow,deny    Deny from all  </Files>  


Solution:2

You can use this to deny access to file types:

RedirectMatch 403 ^/folder/.+\.(pdf|gif|js)$  

This will return a 403 Access forbidden error for clients if they try to access /folder/file.extension


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »