Although we have an article on password protecting directories in the cPanel, there may be times when the structure of your program does not allow the movement of files. This is more common with Content Management Systems such as Wordpress or the Premium Website Builder. You may want or need to protect a single file from random outside access. For example, you may want to set one of your pages up to only be accessed by a certain group. Moving the file to another folder is impossible due to the coding of the site. This article will demonstrate how to password protect a single file via your .htacess file. This allows you to set the access to a specific username and password while keeping the file in the mandatory location.

How to password protect a single file in .htaccess

  1. The first step is to create the username and password you want to use for the access. You want to use an encrypted password for extra security. This is not done via the cPanel, but needs to be done from an external site. Penn State University has a page that will do that for you. Simply enter the desired username and password and click on Submit. It will then produce a line of code to be used later. Be sure to store that line of code somewhere such as a notepad file while as you will need it later to add to your password file.
  2. Next you want to log into your cPanel.
  3. After logging in, enter the File Manager. Be sure to have the Show Hidden Files option connected and enter the File Manager at the Home Directory.
  4. From the Home Directory you will create a new file. While you can name it anything you like, be sure to begin the name with a . (.htpass, .htpasswd, .abc123) In our example, we will name it .htpasswd
  5. Now that the file is created, we need to edit it to insert the password code we generated. Click on the Edit icon found in the toolbar at the top of the page.
  6. Once in the editor, insert the password code you generated earlier and click on the Save Changes button to save it.
  7. We now need to move to the root folder for your domain. In the case of your primary domain, this will be the public_html folder. For addon domains, the folder will be public_html/addondomain.com. Once in the folder, look for your .htaccess file. If you do not see one, you will need to create a new one. Once you find or create the .htaccess file, click on the Edit icon from the upper toolbar.
  8. This takes you to an editor similar to the one used to insert the password code. Copy and paste the following code into the .htaccessfile. This is a template we will edit for the password configuration.
    AuthUserFile /path/to/htpasswd/file/.htpassword
    AuthName "Name of Page"
    AuthType Basic
    <files> require valid-user </files>
  9. Edit the first line, AuthUserFile /path/to/htpasswd/file/.htpassword to represent where the passowrd file is locate. Your code should look like the following (be sure to replace userna5 with your cPanel username).
    /home/userna5/.htpasswd
  10. The next line of code needs to be changed to something recognizable, this will be the title of the login box that will appear when someone attempts to access the file. Below is our example.
    AuthName "Members Only"
  11. The last line of code we need to edit is the <files> require valid-user </files>. Here we will simply add the name of the file in the beginning tag as per our example.
    <files "members.html"> require valid-user </files>
  12. Once you have finished editing the file, your code should look similar to our example below. Click on the Save Changes button to finalize it. Be sure to visit the page to ensure the login request pops up. Once you log in, the site will remember you and not ask again from the same browser unless you clear the browser cache.
    AuthUserFile /home/userna5/.htpasswd
    AuthName "Members Only"
    AuthType Basic
    <files "members.html"> require valid-user </files>
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve our Help Center:
Email Address
Optional, but our team may contact you for more information.
Did you find this article helpful?

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

News / Announcements

Update to SSL Certificates - Certificate Warnings
1969-12-31 11:00 am EST
Hits: 690

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.