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. htaccesstools 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 this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?

Comments

n/a Points
2017-09-25 1:47 pm

Hi. the Step 1 link doesn't work.. Can you help with this.

Thank you

Staff
116 Points
2017-09-25 9:39 pm
Hello,

Thanks for letting us know about the dead link, We have updated it to a new URL that works, It may take a few minutes for the change to show however you can use this URL http://www.htaccesstools.com/htpasswd-generator/ to generate the line of code you need for step 1 now.

Best Regards,
Kyle M
n/a Points
2018-08-10 8:13 pm

When I followed your instructions, it crashed my web site. I got a 500 internal error on all pages of the web site. I then removed the text from the .htaccess file and saved, and my web site came back. This is the code (that I placed at the bottom of the .htaccess file) that broke the web site:

AuthUserFile /home/nqpart5/.ELCpassword

AuthName "Emergent Learning Community Portal"

AuthType Basic

<files "EL_portal.html"> require valid-user </files>

Staff
12,339 Points
2018-08-10 8:26 pm
If you have any other rules in the .htaccess file, they may be interfering with these rules your adding. As a test, you can rename the existing .htaccess file then add these rules to a new one.

Also, check that your hidden file titled "ELCpassword" is in the correct folder location and has the required code.

Thank you,
John-Paul

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

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!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 757-416-6627 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.
}