Password Protected Pages: Htpasswd and Htaccess
Mar 31st, 2008 | By admin | Category: LinuxPassword protected web pages may have crucial importance for you, if you want to hide some of your pages to public. If you have a configured apache server for this, it’s so easy to do the rest to add a password and a user to protect a page or folder.
Here how it is done:
1. Configuring your htaccess
If you do not have a .htaccess file on your website’s directory, create it. But don’t forget that it is a hidden file, if you are using SSH Secure Shell’s browser, you have to select “View” >> ” Show Hidden Files ” to see whether you have created one, before.
Now, add the following code to your .htaccess file.
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /<fullpath>/yourwebsite.com/private-folder/.htpasswd
require valid-user
After AuthName field, write a message that will be seen on the authentication screen and don’t forget to add your fullpath after the AuthUserFile field. It is usually something like this: AuthUserFile /home/.servername/www/yourdomain.com/ .htpasswd
2. Configuring your .htpasswd
.htpasswd is a hidden file that has the encrypted password combinations of your users for authentication. Now you understand what it stands for. On the prompt screen of your linux server, write the following command:
htpasswd -cmb .htpasswd <username>
You’ll be prompted to write your desired password. After that, go to yourwebsite.com/private-folder/ to see if everything is ok. You’ll be asked to login with your username and password.
3. Adding More Users
Adding more users is easy. Just type the following command on the command screen of your server:
htpasswd .htaccess
Type the desired password and that’s all.