How to prevent access to a specific file in htaccess
The .htaccess file is a hidden text file within your hosting account that can be very powerful. It is designed so that you can modify it to change the behavior of your website that normally would take higher access beyond your account. It acts as a liaison between your domain and the server and can perform many functions.
There may be times when you want to restrict direct access to specific files that reside in your account. The server itself will still be able to access the file and use the information. This can be done via the htaccess file. Below are the instructions for preventing access to specific files via the .htaccess file.
Preventing access to certain files via htaccess
- First, you will need to log into your cPanel to access the htaccess file.
- Find the Files category and click on the File Manager icon.
- A popup box will appear. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked. Click the Go button to enter the File Manager.
- You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.
- You are now in the htaccess editor. Paste the following code in the file to prevent access to your selected file. In our example below, we decided to hide the file named filetohide.jpg. This can be a file of any tyep (html, jpg, php, etc.)
# the following prevents display of the filetohide file
deny from all
- Be sure to hit the Save Changes button in the upper right corner to save your new htaccess configuration.
Thank you for your feedback!