Protect website with htpasswd and htaccess

28 May

Sometimes we need to protect our website or website directories with username and password while we are working on development server. Password protect one or more directories with Basic HTTP Authentication using .htaccess. A .htpasswd file will be used to store login details.

Add following code into your .htaccess file

AuthType Basic
AuthName “My Protected Area”
AuthUserFile /path/to/.htpasswd
Require valid-user

Auth name: This name will be displayed in the login box when a user access your protected area for the first time.

AuthUserFile: you need to enter the full path of your .htpasswd file here. Use the following PHP script to find the full path of you .htpasswd file

$dir = dirname(__FILE__);
echo "<p>Full path to this dir: " . $dir . "</p>";
echo "<p>Full path to a .htpasswd file in this dir: " . $dir . "/.htpasswd" . "</p>";
Leave a comment

Posted by on May 28, 2012 in Uncategorized


Tags: , , ,

Leave a Reply

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

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

Google photo

You are commenting using your Google 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

%d bloggers like this: