setup HTTP Authentication using htpasswd and htaccess

First create .htpasswd file for allowed users. You can create .htpasswd file using one of the following ways:

  1. By using command line
    htpasswd [-m | -d | -s | -p] /path/to/passwdfile/ <username>
    Above command will prompt for password. Given password will be assigned to <username>
    -m : Use MD5 encryption for passwords
    -d : Use crypt() encryption for passwords
    -s : Use SHA encryption for passwords
    -p: Use plaintext passwords
  2. By directly editing .htpasswd file
    open the file .htpasswd and add like username:password -> then save and close.
    This will store password in plaintext format.

Ref. URL: http://httpd.apache.org/docs/1.3/programs/htpasswd.html

Now Modify .htaccess which is placed into directory which you want to protect to refer htpasswd file as authenticated user.

Add the following in .htaccess file

AuthName “Restricted Area”
AuthType Basic
AuthUserFile /path/to/.htpasswd # full path to .htpasswd file
AuthGroupFile /dev/null
require valid-user

Now whenever anyone try to access directory or sub-directories in which .htaccess file is place then browser will prompt for HTTP Authentication Popup.

Please Note: Above will only work on Linux OS.

For more information please refer: http://mitulmpatel.wordpress.com/2009/11/20/htaccess-useful-tricks/

Post comment if you have any problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.