Password Protect One File on Your Site

By sbondo1234 on 27 April, 2019

What?

Instead of protecting the whole directory on your site there is a way to protect individual files in that directory.

How?

Before you start: Finish this tutorial here and then come back to complete this one

Ok, once you have finished the other tutorial you can simply add the following code around the code you already have inside of your .htaccess file. Your code inside of your .htaccess file should look something like this (Change dog.html to your filename and type) :

<FilesMatch "dog.html">
  AuthType Basic
  AuthName "Password Protected Area"
  AuthUserFile C:/xampp7/htdocs/i/.htpasswd
  Require valid-user
</FilesMatch>

If you want to make another file password protected then you can copy the code above and paste it below. Then just simply change the name of the file in the speech marks in the top line. If you plan on making two or more files password protected your .htaccess should now look like this:

<FilesMatch "dog.html">
  AuthType Basic
  AuthName "Password Protected Area"
  AuthUserFile C:/xampp7/htdocs/i/.htpasswd
  Require valid-user
</FilesMatch>

<FilesMatch "dog2.html">
  AuthType Basic
  AuthName "Password Protected Area"
  AuthUserFile C:/xampp7/htdocs/i/.htpasswd
  Require valid-user
</FilesMatch>

You can also change the AuthUserFile if you don’t want the same passwords to work on both files.