Just Host Web Hosting Help
Password Protect a File
Summary
The easiest method of password protecting a single file on your hosting account is to first password protect the directory which contains that file. This can be done using the Password Protect Directory tool in your cPanel.
This article will show you how to manually password protect a directory (folder) on your account.
Password Protect a Directory
When you Password Protect a directory, the system will place configurations into a file called '.htaccess'. This file will be located in the folder that you protected.
In the .htaccess file will be statements such as the following:
AuthType Basic
AuthName "Restricted Area"
AuthUserFile "/home/[username]/.htpasswds/public_html/passwd"
require valid-user
You will now need to modify the .htaccess file so that it applies the statements to a specific file. This is done with the following tags:
<Files [filename]></Files >In this example, we will protect the file 'secure.html'. This is done by modifying the .htaccess statement as follows:
<Files secure.html> AuthType Basic AuthName "Restricted Area" AuthUserFile "/home/[username]/.htpasswds/public_html/passwd" require valid-user </Files>
This can also be used to protect multiple individual files in directory, the method is very similar, however this time use Apache’s FilesMatch
directive. This allows us to list as many files as needed:
<FilesMatch "(secure\.html)|(secure\.txt)"> AuthType Basic AuthName "Restricted Area" AuthUserFile "/home/[username]/.htpasswds/public_html/passwd" Require valid-user </FilesMatch>
Note: To add files, include more instances of “|(filename\.ext)”.