You are Here:   FAQ->Scripting & Programming->.htaccess->Article #3


How can I manually password protect my files/folders (Linux)?


For Linux packages with SSH access only!Linux Packages with SSH Access Only!
      This includes:
arrow 1&1 Business Pro (Linux) arrow 1&1 Dual Standard (Linux)
arrow 1&1 Professional (Linux) arrow 1&1 Dual Unlimited (Linux)
arrow 1&1 Managed Servers arrow 1&1 Dual Business (Linux)
      This DOES NOT include:
not 1&1 Beginner not 1&1 Business (Linux)
not 1&1 Home (Linux)




While it is possible to restrict access to the contents of a folder by using the Protected Directories section of the 1&1 Control Panel, this can also be done manually.

The advantages to configuring restrictions manually is:
arrow image You can restrict not only contents of a folder, but individual files as well.
arrow image You can restrict access to multiple files using a separate username/password for each.
arrow image The configuration already present in a .htaccess file will not be completely overwritten by changes via the Protected Directories section of the 1&1 Control Panel.

Step 1 Make sure to change directories to the folder (or create a new one) where you want to password protect files or folders. This could be the root of your webspace or a subfolder. In this example, will we be password protecting a subfolder named 1and1help/protectedDir/ (the folder 1and1help/ is used to hold the files for 1and1help.com).
Step 2 We will now create a username and password that is required to access the folder named "protectedDir/".

Type htpasswd -c .htpasswd john (but replace "john" with a username of your choice) and press ENTER. The -c option specifies to create a new .htpasswd file that will contain a list of usernames and passwords.

Enter a password and press ENTER. As you type, nothing will be displayed on the screen so that onlookers can not see your password or guess at the length. Repeat the password and press ENTER.

protect1.png
Step 3 Now that we have created a file containing the username and encrypted password, we must edit the .htaccess file to specify what folder/files should be password protected, and the location of the file that contains the username and password.

arrow image Type pwd and press ENTER to print working directory (print out the full folder struction to the current location).
arrow image Highlight the folder structure by clicking and dragging the mouse over the text to copy it.

protect2.png
Step 4 Type nano .htaccess and press Enter. This will open up the Nano text editor and create the .htaccess file or edit an existing one.

protect3.png
Step 5 Right-click the window with your mouse to paste the folder structure into the Nano text editor.
arrow image Add a forward slash(/) to the end of the line followed by .htpasswd
arrow image Move the cursor to the beginning of the line and type AuthUserFile and then a SPACE.
arrow image Move down to a new line and type AuthType Basic
arrow image Start another new line and type AuthName "Protected Folder"

Reference the image below against your own text in the Nano editor. Yours should be the same aside from the folder structure.

protect4.png

If everything looks correct, press the Ctrl and x keys at the same time to exit and save the file.
Step 6 Open up your browser and navigate to the password protected URL such as http://www.1and1help.com/protectedDir/ and you should be prompted for the username and password you chose earlier before you can view anything in the folder.

protect5.png
Step 7 Scroll to the bottom of the page for other examples of how to create more users and password protect specific files.




Creating additional usernames and passwords
Make sure you are in the same directory as the .htpasswd file (1and1help/protectedDir/ as in the example above) and type htpasswd .htpasswd becky (but replace "becky" with a username of your choice). Enter the password twice and press ENTER, the repeat the password and press ENTER.



Restricting access to specific files
View the example below for more help on specifying individual files that should be password protected as well as specifying which users that are allowed to access a file.
AuthUserFile /homepages/30/d123456789/htdocs/1and1help/protectedDir/.htpasswd
AuthType Basic
AuthName "Protected Content"

<Files "protectedPage.html">
Require user john
</Files>

<Files "protectedPage2.html">
Require user becky
</Files>

<Files "protectedImage.jpg">
Require user john
Require user becky
</Files>   



Print Article
How useful was this article?
(From 5 = Very Useful to 1 = Not Very Useful at all):
1 2 3 4 5