Password Protect a File


How do I Password Protect a single file on my website?


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.

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

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

Note: To add files, include more instances of “|(filename\.ext)”.

Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Force SSL on all pages. (Views: 3616)
Hotlink Protection (Views: 1231)

Client Login



Remember Me