Protect a file with .htaccess and .htpasswd

↘锁芯ラ 提交于 2019-12-11 07:24:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!