Create a file named ".htpasswd".
The name can be any one that suits you, not necessarily ".htpasswd".
Paste this into the ".htpasswd":
login:$apr1$pfIh.j7l$Zlqiecx1ZoYfEoUn1QVA50
" login " is the user
" $ apr1 $ pfIh.j7l $ Zlqiecx1ZoYfEoUn1QVA50 " is an encrypted string. The password is "pass".
In htaccess, add:
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /caminho/absoluto/do/arquivo/.htpasswd
Require valid-user
That's enough to get started.
To generate the encrypted password, there are hundreds of sites like this: link . Just search the google "htaccess password generator" or you can even generate it without internet use.
* So far we have covered superficially. If you would like to know more about it, please continue reading below.
Generating password
For the Windows environment, in the Apache installation directory, in the "bin" folder, you will find the "htpasswd.exe" executable. From the Windows CMD, go to the folder for this file, for example
cd C:\Apache\httpd-2.4.20-win64-VC14\bin
* The exact location varies by installation on your system.
Thepasswordcanhavedifferenttypesofencryptionandcanevenbegeneratedasplain/text
,thatis,in"plain text", without encryption. The default is MD5. Note that the type of password chosen affects the AuthType
parameter, so if you encrypt the password with another format such as crypt, set the AuthType
equivalent to the encryption used. See the documentation: link
* To generate the password in other environments like linux and mac, follow the same logic. It will only modify the executable path and some features.
Safety Tips
The .htpasswd file path must be in a private location, without public access.
Example, if the site index is in c: /www/site/index.php, place the file in a folder outside the public folder
How NOT to do:
c:/www/site/.htpasswd
In this way, third parties can download the password file. Therefore, avoid placing in a publicly accessible location
Suggestion of where to put it:
c:/www/.htpasswd
Password file naming
The default name is .htpasswd because the default Apache installation checks to see if this file exists and if it exists, it is blocked from external access if it is in a public directory. This is for cases where there is an oversight and leave the file with public access.
Nevertheless, do not fully trust, as not all environments can have the same rule by default. Just in case, leave the file outside the public folder.
For more details on the subject, read the documentation: link
Allow specific files and directories
To allow free access to specific subdirectories or files, add the rule based on the URI:
SetEnvIf Request_URI "(/caminho/completo/do/diretorio1/)$" allow
SetEnvIf Request_URI "(/caminho/completo/do/diretorio2/)$" allow
SetEnvIf Request_URI "(/caminho/completo/do/diretorio3/)$" allow
SetEnvIf Request_URI "(/caminho/completo/aquivo/especifico\.php)$" allow
Order allow,deny
Allow from env=allow
Satisfy any
07.07.2016 / 08:46