How to Password Protect One File

This is an advanced design tutorial. If you are a beginner, we recommend using the cPanel Password Protect Directory icon. That icon can be used to password protect all files in a folder; it cannot password protect one specific file.

To password protect an individual file:

  1. Create a file named .htpasswdand place it above your root directory, so visitors can't access it.Create an encrypted password using either a utility such as the command line program htpasswd or an htpasswd generator. It doesn't matter how many individual files you wish to protect; they can all share a common .htpasswd file.
    • Example of the root directory pathway for shared accounts : /home#/cpanelusername/.htpasswd
      Note: Replace the # with the number that is reflected in your file manager's home directory
    • Example of the root directory pathway for reseller, vps and dedicated server accounts: /home/cpanelusername/.htpasswd
  2. Create an encrypted password using either a utility such as the command line program htpasswd or an htpasswd generator. It doesn't matter how many individual files you wish to protect; they can all share a common .htpasswd file.
  3. Right click on the .htpasswd file and select the Code Edit option.
  4. In the popup that appears, click the Edit button.
  5. Place the generated .htpasswd information into the .htpasswd file.
  6. Click on the Save Changes button.
  7. Click on the Close button.
  8. Navigate to the directory where the file(s) that you would like to password protect lie.
  9. If an .htaccess file does not exist in the directory already, create one.
  10. Right click on the .htaccess file and select the Code Edit option.
  11. Insert the following code into the .htaccess file:
    <FilesMatch "examplefile.extension">
    AuthName "Member Only"
    AuthType Basic
    AuthUserFile /home/cpanelusername/.htpasswd
    require valid-user
    </FilesMatch>

    Be sure to replace replace examplefile.extension with your file's actual name and replace cpanelusername with your actual cPanel username.
  12. Click on the Save Changes button.
  13. Click on the Close button.

This will protect the specified file, while leaving all other files in the folder unprotected.

WordPress and Password Protecting Files

WordPress can have some minor conflicts with password protecting files due to the changes in the .htaccess rules. If you are experiencing issues with this, please refer to the following article:

  • WordPress Preventing Password Protection
Password protecting a subdirectory using the cPanel icon (or a .htpasswd file) will be unsuccessful if WordPress is installed in the public_html directory with permalinks enabled.

The following article will provide instructions for password protecting a directory in WordPress and resetting the permalinks once that has been done. This will involve editing the .htaccess file. Please review the following article for further information regarding how to edit your .htaccess file.

Before making any changes to your .htaccess file, it is suggested that you backup your website so that you can revert back to a previous version should something go wrong.

Edit .htaccess File

  1. Edit the .htaccess file in your WordPress home folder (public_html) and remove the line  of code shown in bold below:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngineOn
    RewriteBase/
    RewriteCond%{REQUEST_FILENAME}!-f
    RewriteCond%{REQUEST_FILENAME}!-d
    RewriteRule./index.php [L]
    </IfModule>
    # END WordPress
  2. Replace that line with this new line:

    RewriteRule.//index.php [L]

    The final code snippet will look like this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngineOn
    RewriteBase/
    RewriteCond%{REQUEST_FILENAME}!-f
    RewriteCond%{REQUEST_FILENAME}!-d
    RewriteRule.//index.php [L]
    </IfModule>
    # END WordPress
  3. Save your changes.

Reset Permalinks

  1. From the left-hand navigation menu in WordPress, click Settings > Permalinks. Note the current setting. If you are using a custom structure, copy or save the custom structure somewhere.
  2. Select Default.
  3. Click Save Settings.
  4. Change the settings back to the previous configuration (before you selected Default). Put the custom structure back if you had one.
  5. Click > Save Settings.

This should reset the permalinks and prevent any posts or pages from returning a 404 Page Not Found error. You should now be able to password protect a subdirectory.

Password Protect Multiple Files

You can protect more than one file by using wildcard names. On the FilesMatch line in the previous .htaccess code example, try one of the directives below:

  • <FilesMatch "*.html">

  • <FilesMatch "file.*">

  • <FilesMatch "*.*">

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Log into cPanel

cPanel is a web hosting account management tool available on Linux hosting plans, including...

How do I deny IP address access?

How do I deny IP address access? Do you want to stop someone from visiting your website? This...

Viewing Your Disk Space Usage

Viewing Your Disk Space Usage To view the disk space usage totals for your account's...

Requesting a New cPanel Password

If you have lost or forgotten your cPanel password, you can reset it using any one of the methods...

How to Change the Password of An E-mail Account

How to Change the Password of An E-mail Account You can make changes to your email account...