Question

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.

Était-ce utile?

La solution

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>

Autres conseils

I think you need to put complete path to htpasswd file: Ie.

AuthUserFile /home/-user-/-site-/.htpasswd

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top