How to password protect a single file? Print

  • 66

In order to password protect a single file, you need to have the following files under your account:

.htpasswd (containing the username and password through which the file can be accessed)

.htaccess (containing the authentication rules)

You can create both files through your cPanel > File Manager.

You should follow these steps:

1) Do NOT create the .htpasswd file below your public_html directory. Otherwise it will be

accessible through http requests. We would advise you to use the

root folder for your account - /home/username/ (where username is your cPanel username).

In this file you should put the encrypted password for your username. This can be

done using the following username/password generator:

http://www.htaccesstools.com/htpasswd-generator/

2) Put the .htaccess file in the directory of the file that you

wish to be protected. Please note that .htaccess files work recursively

therefore those changes will affect all the subfolders in the directory

that you have placed the .htaccess file in. Once the .htaccess file is in place, the

following lines should be added to it:

<FilesMatch "file.html">

AuthName "Member Only"

AuthType Basic

AuthUserFile /home/username/.htpasswd

require valid-user

</FilesMatch>

In the <FilesMatch "file.html"> part you specify the criteria for

protecting your files. You can choose a single file:

<FilesMatch "file.html">

or files from different types but with the same name:

<FilesMatch "file.*">

In the AuthUserFile /home/username/.htpasswd part you should change username to your cPanel username.


Was this answer helpful?

« Back