How to block direct access of File using htaccess


In this htaccess tutorial, we are going to show how you can prevent the direct access of file/URL of your website. Sometimes website owner doesn’t want that user can access their zip, CSS, etc file directly.

To prevent the direct access, you need to add the below script in the .htaccess file.

.htaccess
RewriteEngine on

# script to  stop direct link of zip and css file
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example[NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.*$ [NC] 
RewriteRule \.(zip|css)$ http://www.example.com[R,L]

You can add multiple file extensions by ORing the pipe(|) symbol.

(gif|zip|png|js|css|bmp)

About the Author

Atul Rai
I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.