Password protect a site with .htaccess

Despite htaccess being around forever, I always forget how to implement it. This is a simple "just get it to work" implementation.

akonkol
February 24, 2012

/var/www/<site_name>, etc..

/etc/apache2/sites-available/<file_name>

#first you need to create a password file and a username for access to the site
htpasswd -c /etc/apache2/.htpasswd %{username}%


#Second you need to create a ".htaccess" file in the directory you wish to protect
vi %{path_to_protected_dir}%/.htaccess
AuthType Basic
AuthUserFile /etc/apache2/.htpasswd
AuthName "Enter password"
Require valid-user


#Third you need to add the "AccessFilename" directive in your virtual host file
vi /etc/apache2/sites-available/%{website_config_file}%
<VirtualHost *:80>
...
AccessFileName .htaccess
...
</VirtualHost>


#Fourth you need to restart your webserver
/etc/init.d/apache2 restart