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

  1. First, you will need to log into your cPanel to access the htaccess file.
  2. Find the Files category and click on the File Manager icon.
    cpanel

  3. In order to select a different domain click on the Settings in the top right corner. 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 Save button to enter the File Manager.
    file-manager-show-hidden

  4. 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.
    file-manager-htaccess

  5. 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
    <files filetohide.jpg>
    order allow,deny
    deny from all
    </files>
  6. Be sure to hit the Save Changes button in the upper right corner to save your new htaccess configuration.

13 thoughts on “How to prevent access to a specific file in htaccess

  1. I want to hide my sitemap from competitors. not for search engines. Is it possible above-mentioned .htaccess tutorial? Please help me with this to enhance the developer’s knowledge.

    1. You could try renaming the file to something difficult to guess and submitting it to search engines afterwards. You can see other options within this Stack Exchange forum. I hope this proves helpful for you.

      1. Hi, so hide my sitemap from competitors, not for search engines, this method is not recommended?

      2. I’d say it’s more about your comfort doing the steps. If you’re uncomfortable following some of the ideas from the StackExchange forum above, you may be better off not trying it for now. The priority should be to have a completed sitemap visible to search engines.

Was this article helpful? Join the conversation!