Password Protecting Individual Files

This page is showing a generic answer.
To see a more detailed answer customized for you, type your domain name here:

It's possible to create a .htaccess file so that individual files are protected instead of protecting a whole directory (folder).

However, you should only attempt this if you are an advanced user and familiar with .htaccess files and the syntax they use. Unless there is a specific reason why you need to protect a single file, we wouldn't normally recommend it. In our experience, creating a separate folder for sensitive files is a far simpler and less error-prone.

If you do decide to protect an individual file, you can do so by adding a FilesMatch directive to a .htaccess file. For instance, the following code will protect the file called secrets.html:

<FilesMatch secrets.htm>
   AuthName "Restricted File"
   AuthType Basic
   AuthUserFile /home/ex/example.com/webpasswords-passwordlist
   require valid-user 
</FilesMatch>

As with all .htaccess files, the rules will also be applied to any subdirectories unless they are overridden by additional .htaccess files.