Guide To Password Protect A Website File

Password Protect A Website File

Directory folders can be password protected in cPanel by clicking the Directory Privacy icon within the Files management section. This will secure the entire folder and all files within the folder.

If you only want to protect one file, then this guide will show you how to protect the file while leaving all other files in the folder unprotected. 

Looking to password protect just one website file?

Let's Get Started!

  1. First, you will need to create an encrypted username and password using this free Htpasswd Generator Tool
    • In field one, type in your username
    • In field two, type in your password
    • Click the create .htpasswd file and copy the encrypted code - The actual name and password will not be visible, but hashed encrypted for protection
  2. Now, create a file named .htpasswd and past ethe code from the previous step into the file and save the file
    • If you have multiple username/passwords, repeat step 1 as often as needed and paste the code into the .htpasswd file on seperate lines.
    • Note: If you have more than one file you want to password protect, they can all share the same common .htpasswd file.
    • Upload the .htpasswd file above your root directory so visitors can not access the file. Example: /home/cpanelusername/.htpasswd
  3. Open your .htaccess file in the directory where the file you want to protect is located. If the file is at the top level /public_html/ folder then the .htaccess should already exist, otherwise you will need to create one.
    • Insert the code below into your .htaccess file:

      # Begin Password File Protection
      <FilesMatch "protect.htm">
      AuthName "Member Only"
      AuthType Basic
      AuthUserFile /home/cpanelusername/.htpasswd
      require valid-user
      </FilesMatch>
      # End Password File Protection

    • Replace protect.htm on the second line with the name of the file you want to lock
    • Replace¬†cpanelusername on the fifth line with your cPanel username
    • Save the file and upload to the server.
  4. Now open a browser and enter the url of the page you wanted to protect. You should be prompted to enter a username and password to gain access.

SHARE THIS POST: