问题
I'm having some troubles configuring the .htaccess file to protect the access of a file. The file to protect is: www.mydomain.com/admin/stats.php
I put the .htaccess file into the www.mydomain.com/admin folder with the following code:
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /
<Files stats.php>
require valid-user
</Files>
The file .htpasswd is into the www.mydomain.com/admin folder too and it contains the username and the password.
If I try to access to www.mydomain.com/admin/stats.php I get an "Internal Server Error". I think that the mistake can be into the path of "AuthUserFile" but how can I tell the file that the file to protect is in the same folder of the .htpasswd?
The server is Linux based.
回答1:
hello you can try this below :
this is a path.php file who will display the realpath of his parent folder
<?php
echo realpath('path.php');
?>
in your .htpasswd you need to have something like this :
username:password
but the password need to be encrypt, you can do it with
<?php
echo crypt('your_password');
?>
and finally this is the .htaccess file
<Files page_to_protect.php>
AuthName "Message"
AuthUserFile \realpath_to_htpasswd\.htpasswd
AuthType Basic
require valid-user
</Files>
回答2:
I think you need to put complete path to htpasswd file: Ie.
AuthUserFile /home/-user-/-site-/.htpasswd
回答3:
From the Apache documentation:
File-path is the path to the group file. If it is not absolute, it is treated as relative to the ServerRoot.
That means you either specify the full (absolute) path to the .htpasswd
file or one relative to whatever your ServerRoot
is. Example:
AuthUserFile /www/passwords/.htpasswd
来源:https://stackoverflow.com/questions/11283866/protect-a-file-with-htaccess-and-htpasswd