Monday, March 12, 2012

Password Protecting Your Pages with htaccess

Creating the password file using htpasswd

If you have SSH access to your web server (or you're running Apache on a local machine), you can encrypt your password and add it to your password file in one go by using the 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 -c .htpasswd fred
(where fred is the username you want to use). You'll be prompted to enter and retype your password, then the .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.


3 comments:

  1. I recently discovered a freeware called File Secure Free. It is a freeware to password protect folder, file and drive. It works great. I should say it can even beat some similar paid products. Besides this, it also provides file backup, USB encryption and file shredder features.

    ReplyDelete
  2. thanks for sharing! very useful!! :)
    btw, i also have a blog and a web directory, would you like to exchange links? let me know emily.kovacs14@gmail.com

    ReplyDelete
  3. First time to try htaccess to password protect folder files. thank you very much.
    http://www.kakasoft.com/folder-protect/

    ReplyDelete