How to password protect single files with htaccess

How to password protect single files with htaccess

Protecting directories with htaccess is easy and comfortable. This tutorial shows how to protect single files only. more >>

Contents

Create a password file

On unix systems you can use the htpasswd / htpasswd2
command:

htpasswd2 -cb PASSWORD_FILE USER PASSWORD

PASSWORD_FILE is the file where the passwords are
stored.

USER is the username

PASSWORD is the password

htaccess code

Put the follwing code into the .htaccess file:

<FilesMatch "file.html">
AuthName "Members Only"
AuthType Basic
AuthUserFile /absolute_path/to/PASSWORD_FILE
require valid-user
</FilesMatch>

file.html is the file you want to password protect.

"Members Only" is the String that appears on prompt window

/absolute_path/to/PASSWORD_FILE is the path to the password file you have specified on step 1.