Password protect a file in cPanel

You can easily password protect directories in cPanel by making use of the ‘ Password Protect Directories’ utility present under the Security tab. But is it possible to password protect a single file alone?? Lets find out.

To password protect a file, follow the below steps.

1) Create a file with the name .htpasswd (notice the . in front of htpasswd, it is not an editing mistake. ‘.’ shows that the file is hidden and the file is to be created as specified) and place it above your root so visitors can’t access it.

Example:

/home/myuser/.htpasswd

2) Put the username (username to be given while unlocking the file)and encrypted password (wait, what? which encrypted password? that’s what you are thinking right 😉 follow the link below to create the encrypted version of the password required 🙂 )inside the .htpasswd file.

To create the encrypted password, use an online utility, such as htpasswd-generator

Example:

user:$apr1$KdsvL5X8$uc0gsDV9uRMM.vgpVCHV30

You do not have to create individual .htpassword files for each file/directory to be protected. You can share the same .htpassword file for all the files. Just add the new entry in a new line.

Example:

user1:$apr1$KdsvL5X8$uc0gsDV9uRMM.vgpVCHV30


user2:$apr1$pd6GgEv9$C7fJUQLy2QwhJil5biBNE1

3) Place the following code in your .htaccess file, in the same folder where you want to protect the file. (Be sure to replace myuser with your actual cPanel username. Replace dir with the path to your file. Replace myfile.php with your file’s actual name.)

EXAMPLE:

Open the file

/home/myuser/public_html/dir/.htaccess

and add the lines

<FilesMatch “myfile.php”>

AuthName “Member Only”

AuthType Basic

AuthUserFile /home/myuser/.htpasswd

require valid-user

</FilesMatch>

You can protect more than one file by using wildcard names. On the FilesMatch line, follow one of these examples:

<FilesMatch “*.html”>

<FilesMatch “file.*”>

<FilesMatch “*.*”>

 

Thats it 😉 Simple, isnt it 🙂

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.