
At present, most of the Websites and blogs are hosted on Apache Server, which provides directory-level configurations for fine tuning and efficient performance of the website. Apache uses Hypertext Access files, that .htaccess file, which configures Apache’s Universal system-configuration directives.
Usually, a website or a blog, contains several private and sensitive information, and kept confidential for outside world. As, in my last article on Wordpress Security: Disable directory browsing using htaccess on Apache Web Server, directory browsing is enabled by default and have to disable it using .htaccess method. But disabling a directory means, even the administrator can’t access the contents, which can rate the height of stupidity. But, if we password protect web directory having confidential information, the directory will be disabled for the outside world, and user knowing the password, can gain access to its contents.
On apache server, uses .htaccess files to set directives in order to disable access or password protect directory. .htaccess, uses another file which, consists of usernames and passwords, allows successful authentication to directory. Below is the procedure to create a .htaccess file:
- Browse to directory which need to be password protected.
- Create a file .htaccess within the directory, for instance, if I want to protect “www.youdomain.com/life”, I’ll create .htaccess file under the “life” sub-directory under root directory of the domain “yourdomain.com”.
- Copy-Paste the below code snippet:
- Now save the file.
1: AuthUserFile /www/path/to/file/.htpasswd
2: AuthGroupFile /dev/null
3: AuthName "Confidential Information"
4: AuthType Basic
5: require user joe
.htaccess, uses another file which, consists of usernames and passwords, allows successful authentication to directory.
- Create a text file that will store username and password parted by a colon (:).
- Simply copy-paste the code in below snippet, to your htpasswd file.
- Save the file as by .htpasswd or any other name, but make sure you edit the .htaccess file accordingly.
1: aky:admin
2: joe:going
3: akshay:khurana
Once all the above configurations are set, upload the files via ftp if you created them locally at your end. Once the files are uploaded, the directory restrictions are set, and user will be prompted to access that specific directory having the .htaccess file. Below is a screenshot prompting the username and password for authentication.
If the login authentication is successful, user is redirected to the protected web directory, else it is redirected to 401 Authorization Required Exception Page.
This concludes the article as we learned how to password protect our web directory using .htaccess.
For any queries or further assistance required, post comments as your feedbacks.











