How to password protect a single file?

The easiest way to password protect a single file is to place it in a folder of its own and then follow the steps here to protect with a password the entire folder.

Alternatively you can only protect a single file in any directory. To do that you need to first use this tool to encrypt the desired password. It will provide you with a line that looks like this:

Then you need to create a file named .htpasswd inside the home folder of your account (/home/username) and place the line you generated with the tool above inside it. You can create and edit the file via the File Manager tool in cPanel.

Next you need to create a new file named .htaccess inside the folder where the file you want to protect is located. If there is such a file present already, you can only edit it without overwriting the existing rules inside it. In the file, add the following lines:

In the above, you need to change file.html to the name of the file that you want password protected. Also change username to your cPanel username.

Both the .htpasswd and .htaccess files are hidden files and you can check our detailed article how to edit them.

You find this article useful? Click here to learn more about SiteGround web hosting experts and what else we can do for you!