0

How can I add .htaccess? I have two files it is .htaccess and .htpasswd. But this files work only when they are in folder admin. How can I add it only for admin. admin.php is in main directory. I mean that admin.php is not in folder admin.

.htaccess

AuthType Basic  
AuthName "For admins only!"
AuthUserFile /var/www/.../.htpasswd
require valid-user

.htpasswd

admin:$apr1$vVd1NLts$q4Grxt/...
AleBuo
  • 195
  • 1
  • 9

1 Answers1

0

How to protect one file with htaccess and htpasswd

To protect only 1 file, but not all the directory, you need to have two files:

Example file: mysite.com/oneFile.php

First this lines in you root .htaccess

ErrorDocument 401 "Unauthorized Access"
ErrorDocument 403 "Forbidden"
<FilesMatch "oneFile.php">
AuthName "Authorized Only"
AuthType Basic
AuthUserFile "/home/mysite/public_html/.htpasswd"
require valid-user
</FilesMatch>

Now the file .htpasswd in the same directory of the .htaccess and the same of the oneFile.php:

This is the example user and pass for oneFile.php:

admin:$apr1$1kBnr3C4$ndhru5WIWlJdSFIn9F.gI0

Attention: To create this password do you need some htpasswd Generator

I hope you find it useful.

AleBuo
  • 195
  • 1
  • 9