Protect Folder On Apache With Username And Password Login Using .htaccess

July 31, 2019 by Andreas Wik

There is an easy way to password protect folders on your Apache server with username and password.

First, create a new file inside the folder named .htaccess and add the following, which will prompt the user with a login window.

AuthName “New Design Protected Folder“
AuthUserFile .htpasswd
Require valid-user

 

Next, we need to create another file in the same folder which we reference on line 3 above: .htpasswd. You can place this file in another folder if you wish, but then you’ll need to provide the full path, e.g. AuthUserFile /folderA/folderB/.htpasswd.

This file will include our username and an encrypted version of our password, looking something like this:

andreas:$apr1$1UAUwPAy$BojC0IKScOJcW8cLyTJnK0

 

Go here to generate the username and password line for your .htpasswd file as seen above.

Upload these two files to the folder on your server, restart the server, and you should now be prompted with the following when you try to visit it:

 

Create more than one login

In .htpasswd you can create as many logins as you wish. One per line.

anne:$apr1$1UAUwPAy$akiC0IKScOJcW8cLyTJnK0
peter:$apr1$1UAKwPAy$BojC0adicOJcW8cLyTJnK2
li:$apr1$1UAOwPAy$RofC0IKScOJcW8cLyTJnK4

Great stuff 🙂

Share this article

Recommended articles