Skip to main content

How to Password Protect One File

This is an advanced design tutorial. If you are a beginner, we recommend using the How to Enable HostGator Directory Privacy Protection guide. This feature allows you to password-protect specific folders on your website, ensuring that only authorized users can access them. To password-protect an individual file:

  1. Create a file named .htpasswd and place it above your root directory, so visitors can't access it.
  • 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
  1. 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.
  2. Right-click on the .htpasswd file and select the Code Edit option.
  3. In the pop-up that appears, click the Edit button.
  4. Place the generated .htpasswd information into the .htpasswd file.
  5. Click on the Save Changes button.
  6. Click on the Close button.
  7. Navigate to the directory where the file(s) that you would like to password-protect lie.
  8. If an .htaccess file does not exist in the directory already, create one.
  9. Right-click on the .htaccess file and select the Code Edit option.
  10. 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 `_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 changes in the .htaccess rules. If you are experiencing issues with this, please refer to the following article:
* WordPress Preventing Password Protection

* * *
## **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:
* ```apache
&lt;FilesMatch "*.html"&gt;

<FilesMatch "file.*">


* ```apache
&lt;FilesMatch "*.*"&gt;

Using Strong Passwords

It is always essential to use strong passwords. To help you create strong passwords to keep your services safe, please refer to the following article:

  • How Can I Make a Stronger Password?