How to prevent a PDF file from direct URL Access in Wordpress?

时间:2019-03-19 14:39:58

标签: php wordpress .htaccess pdf

In my website i have storing user resume in Wordpress upload folder. I have created sub folder resumeuploads under the wordpress wp-content/uploads/ folder. Now the resume are storing on the resumeuploads folder by using PHP code. Now i want to restrict the public access of PDF files on my website. If enter the http://www.example.com/wp-content/uploads/resumeuploads/my.pdf link on browser i can access the PDF files now i want to restrict that direct access. I have tried below code in htaccess but its not working for me. Could you please help me to solve the issue .

Code 1

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] 
RewriteRule \.(gif|pdf)$ - [F]

Code 2

RewriteCond %{REQUEST_URI} \.(pdf|zip)$ [NC]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

Or is there any way to redirect when accessing the pdf file url into home page???

1 个答案:

答案 0 :(得分:0)

If you want to deny access to all pdf files in the folder, try this in .htaccess:

# Deny access to files with extensions .pdf
<FilesMatch "\.(pdf)$">
Order allow,deny
Deny from all

</FilesMatch>