For password protecting an individual file, follow the below steps:
1. Login to your cPanel account.
2. Go to Files and click on File Manager.
3. In your root directory, create a file with the name .htpasswd. This won’t be accessible to visitors.
- Here’s an example of the root directory pathway for shared accounts: /home#/cpanelusername/.htpasswd
- Here’s an example of the root directory pathway for reseller, VPS and dedicated server accounts: /home/cpanelusername/.htpasswd
- In this tutorial, the root directory pathway will be: /home/idigitalcamp/.htpasswd
4. Create an encrypted password with the help of either using the command line program htpasswd or an htpasswd generator. The number of individual files you wish to protect won’t matter; all those can share a common .htpasswd file.
5. Here we will create an encrypted password with an htpasswd generator. When you go to the htpasswd generator, you will be asked to enter the username and password. Then click on the Create .htpasswdfile button as below:
6. You will get the encrypted password for the username as “idigitalcamp:{SHA}URw6KZPcsyJ6d7RjdXWhKEpCI0s=”
7. Then go back to the root directory and right click on the .htpasswd file and select the Edit option.
8. The edit confirmation popup appears, click on the Edit button.
9. Insert the generated encrypted password information above into the .htpasswd file.
10. Click on the Save Changes and Close button to close the window.
11. Go to the directory where the file(s) that you want to password protect is present. Here we are protecting the test.php file present in the public_html -> test folder. For this, you will need to paste the below code in .htaccess file of the folder where the test.php file exists. If an .htaccess file isn’t there in the directory, you will need to create it.
12. Right click on the .htaccess file and click on Edit.
13. Again click on Edit for confirmation to edit and then paste the below code:
<FilesMatch "examplefile.extension"> AuthName "Member Only" AuthType Basic AuthUserFile /home/cpanelusername/.htpasswd require valid-user </FilesMatch>
Note: Replace examplefile.extension with your file’s actual name and replace cpanelusername with your actual cPanel username.
14. Then click on the Save Changes button and again click on the Close button.
15. With this, the specified file will be protected with password and other files in the folder will remain unprotected.
16. To check if the file is password protected, we will browse here: https://idigitalcamp.com/test/test.php. Here replace idigitalcamp.com with your actual domain name and test.php with your file that you have password protected during the process.
17. Enter the username and password and click on OK/Sign-in. Note that you use the original password used in htpasswd generator and not the encrypted password.
18. You will see the content in that file as below:
In this way, you can password protect one file from cPanel.