Password Protect a File

Problem:

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

Solution:

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
    </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)”.

  • 32 Users Found This Useful
Was this answer helpful?

Related Articles

URL redirect/rewrite using the .htaccess file

Problem: How do I perform a URL redirect/rewrite using the .htaccess file? Solution: .htaccess...

How to host the Primary Domain from a subfolder (.htaccess)

How do I make a sub directory (or sub folder) act as the public_html for your main domain? The...

Redirects that do not work due to PHP variables

Problem: The redirection of www.yourdomain.com/default.html or index.html to...

Redirect

Problem: How do I create a redirect? Solution: The Redirects tool will allow you to redirect...

Restrict subdomain access to addon domains

Problem: How do I stop people from being able to use the addon domain as a subdomain of the...