Password Protect a folder or page using apache

Make sure you have AllowOverride All

First Create any dummy file at any location.For eg i have created pass.txt in /ete/ folder

touch /etc/pass.txt

In .htaccess write this code

AuthName "Restricted"
AuthType Basic
AuthUserFile /etc/pass.txt
Require valid-user

Then Write this command
htpasswd -mc “path to file” username(any username)

For eg htpasswd -mc “/etc/pass.txt” admin

Then it will prompt for password.Give any password.

If we want to give permission on a particular file in that case we need to write this code

<FilesMatch “admin.php$”>
AuthName “Restricted”
AuthType Basic
AuthUserFile /etc/pass.txt
Require valid-user
</FilesMatch>

Advertisements

About Kapil Chugh

In love with PHP and tweaking WordPress. Hungry for Open Source and new technologies. Honest. God-fearing. Happiest when solving problems.
This entry was posted in Apache, Ubuntu and tagged , , . Bookmark the permalink.

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 )

Google+ photo

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

Connecting to %s