How to Password protect directory of your website

There are situations where we want to give access to authorized users only for certain part of your website. In this tutorial we will password protect directory of your website using htaccess . Let us setup httpd.conf so that we can restrict access to Apache web server and allow only Authorized users.

Password protect directory of your website

# vim /etc/httpd/conf/httpd.conf

Scroll down httpd.conf until you see the second occurence of below text and not the first occurence:

AllowOverride None
Change to AllowOverride AuthConfig

Example

<Directory "/var/www/html">
Options Indexes FollowSymLinks
#AllowOverride None  #change None to AuthConfig
 AllowOverride AuthConfig
 Order allow,deny
    Allow from all

</Directory>

Lets Password protect /var/www/html/training directory

Go to directory which you want to password protect and create a file .htaccess with following content

# vi .htaccess
AuthName "Restricted Access"
AuthType Basic
AuthUserFile /var/www/html/training/.htpasswd
require valid-user

Create username and password with htpasswd command

# htpasswd -c /var/www/html/training/.htpasswd user1
password protect

Enter your username and password when prompted.

Conclusion

We have successfully password protected our directory with htpasswd. This will allow you to protect your important documents or files which you don’t want to be accessible publicly. You can read more about htacess from here.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments