Creating the password file using htpasswd
htpasswd
utility that comes with Apache. Simply SSH to your server or open up a terminal window on your local machine, cd to the folder where you want to create your password file, and type:.htpasswd
file will be created for you.Protecting a folder
To password protect a folder on your site, you need to put the following code in your.htaccess
file:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Folder"
Require valid-user
/full/path/to/.htpasswd should be the full path to the .htpasswd
file that you uploaded earlier. The full path is the path to the file from the Web server's volume root - for example, /home/username/.htpasswd
or C:\wwwroot\username\.htpasswd
. (If you're not sure of the full path to your site or home directory, ask your Web hosting company for this info.)The above
.htaccess
file will password protect all files in the folder that it is placed in, and all sub-folders under that folder too. So if you wanted to password protect your entire site, you would place the .htaccess
file in your Web root folder.Protecting a file
To password protect just a single file in a folder, use the following.htaccess
file:
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Page"
<Files "mypage.html">
Require valid-user
</Files>
This will password protect just the mypage.html
file in the folder where you put the.htaccess
file.