How to protect a folder in your website with .htaccess ?

You can protect a folder or your entire website with username and password. You can block all the unwanted users from accessing your protected folder or website. Create a file named .htaccess and place it in the folder you want to protect. In this example I will protect the entire website and I will place the file in /var/www/yourwebsite.com/public_html/.htaccess.

This is the content of the file .htaccess:

1
2
3
4
5
AuthUserFile "/var/www/yourwebsite.com/.htpasswd"
AuthGroupFile /dev/null
AuthName "Private Area"
AuthType Basic
require valid-user

Now type: chmod 644 /var/www/yourwebsite.com/public_html/.htaccess Create a file named .htpasswd in /var/www/yourwebsitel.com/.htpasswd and write your username and the generated password in this format:

1
username:generated_password

Now type: chmod 644 /var/www/yourwebsite.com/.htpasswd Edit the .conf file of your domain in the apache directory (/etc/apache/sites-availables/yourwebsite.com) as follow:

1
2
3
4
5
<Directory /var/www/yourwebsite.com/public_html/>
...
AllowOverride AuthConfig
...
</Directory>

Finally reload the apache: /etc/init.d/apache reload

Random Posts

Previous Posts