.htaccess password protect directories

From Imaginet FAQ
Jump to navigation Jump to search

This tutorial will explain how you can password protect a specific directory, so only authorized users can access that directory. There is an Online .htaccess Password Generator that simplifies this which can be found on this website [htaccess Generator]

The instructions are quite clear, but remember that the path to where you store the .htpasswd file needs to be

/var/www/vhosts/yourdomainname.co.za/somedirectory/

Please find the example that I did below.

Step 1: Insert the below code into your .htaccess file, and upload this file to the directory you wish to protect (or the directory which the files you wish to protect is under): .htaccess:

AuthName "Restricted Area" 
AuthType Basic 
AuthUserFile /var/www/vhosts/designyourwebsite.co.za/somedirectory/.htpasswd 
AuthGroupFile /dev/null 
require valid-user

Step 2: Save the below code into a file called .htpasswd, and upload this file to the location as specified in the "AuthUserFile" line above. It contains your username(s) and encrypted password(s). Important: .htpasswd should be put in a non public folder, such as directly above your public HTML folder, to prevent visitors from viewing this file in the browser. .htpasswd:

exampleuser:ipNgs4Ag1NdmA