How to deny access to a specific file on your site via .htaccess

Do you want to keep a file on your web hosting account private? You can do so by blocking access to it via a rule in the .htaccess file. The steps are:

1. Login to your cPanel account.

2. Go to File Manager – in the FILES Section

3. In the File Manager go to the Settings button on the top right corner.

4. On the Preferences window that will appear, check the Show Hidden Files (dotfiles) option. Click the Save button to apply the settings.

5. Now navigate to the .htaccess file. If the file doesn’t exist you will need to create it.

6. Click the Edit button from the File Manager top menu.

7. Add the below lines to the .htaccess file. Click the Save Changes button and then the Close button.

#block access to the file blockedfile.html; permit access to all other files 
<files blockedfile.html>
Order Allow,Deny
Deny from all 
</files> 

8. Now access to the file blockedfile.html will be blocked. Users will see a 403 Forbidden error.

To block access to specific files (like images), use these lines instead:

#block access to image files - files with jpg/gif/png/jpeg extensions
<FilesMatch "\.(jpg|gif|png|jpeg)$">
Order Allow,Deny 
Deny from all 
</FilesMatc>

Notice that many PHP scripts (like WordPress, Drupal, MediaWiki etc) need the .htaccess files to function properly. Don’t delete these files. Also take care when editing them.

Check the video tutorial we made:

Deny file access

You can also edit .htaccess files on your web hosting account via an FTP client like FileZilla.

This Post Has 2 Comments

  1. Zoya Khan

    i want to disable download for direct link. User must visit site first only after click on download button he can download the file.

Leave a Reply