Home
/
Website Help
/
Other
/
How to password protect a single file?

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.

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:

user:$apr1$2jnjGymj$LAZzF2evDTxx4DkKviREM1

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 in Site Tools.

Next, 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:

ErrorDocument 401 default
<FilesMatch "file.html">
AuthName "Member Only"
AuthType Basic
AuthUserFile /home/customer/.htpasswd
require valid-user
</FilesMatch>

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

Both the .htpasswd and .htaccess files are hidden files. Check our detailed article on how to edit them.

Share This Article