Problem Password Protecting a Specific Page/File with .htpasswd and .htaccess

61 Views Asked by At

I have a .htpasswd file I created in my home directory, and an existing .htaccess file for all of my site pages. I am trying to password protect only one of these pages.

I tried grouping a new .htaccess file with the file/page I am trying to protect in a subfolder / subdirectory, but the pages start to malfunction (maybe because I used a website builder?).

When I add only the code below into the .htaccess file for the entire website without any more commands, the password protection works / functions properly with the right username/password, but the entire site is password guarded. I am unsure how to target only the one file/webpage rather than targeting my entire website! I have tried a couple different commands such as Location, Files, FilesMatch, but the password protection disappears entirely when I use them.

AuthType Basic
AuthName "Please Log In"
AuthUserFile /home/usrname/.htpasswd
require valid-user

Anything else I can try?

1

There are 1 best solutions below

0
arkascha On

This probably is what you are looking for:

<FilesMatch "protected.file.php">
  AuthName "Please Log In"
  AuthType Basic
  AuthUserFile /var/www/.htpasswd
  Require valid-user
</FilesMatch>

Note that you need to specify an absolute path to the ".htpasswd" file. And that this file should be stored outside the DOCUMENT_ROOT location inside the file system for security reasons.