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 control panel.
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. This would be done as follows:
<Files secure.html stillsecure.html secret.php members.shtml>
AuthType Basic
AuthName "Restricted Area"
AuthUserFile "/home/[username]/.htpasswds/public_html/passwd"
require valid-user
</Files>