Learn htaccess password protect a directory rewriterule flags , syntax using the sample example below.
Password Protect a Directory
Password protecting a directory is easy to do, all you have to do is :
- Add some code to your .htaccess file
- Create a file called .htpasswd
- Select a username and generate an encrypted password and then add them to your .htpasswd file
Code to add to your Htaccess
You need to add the following code to your .htaccessfile.
AuthName "Name of your secure area”
You need to edit the file accordingly.
- Name of your secure area = You can call this anything you want ie. Secure Area or Members Area or whatever.
- Full Path To Your Directory = This is the absolute path to the directory where your .htpasswd file is saved.
Here’s an example :
AuthName "Private Area"
Create a file called .htpasswd
You create a .htpasswd file the same way you created the .htaccess file. All you need to do is create a blank document and save it as .htpasswd.
For security reasons, it is best to place this file above the root of your domain ie. place it in something like /home/mysite/ instead of /home/mysite/public_html.
Create a username and password
The username and password added to your .htpasswd file is in the format :
So my generated password might be something like
So how do I encrypt my password in this way? Well there are a number of ways but the quickest and easiest is to use one of the many encryption sites on the web. Just use any of the scripts below to generate your encrypted password.
- htpasswd encryption
- .htpasswd Content Generator
Once you have your username and password, simply add the line to your .htaccess file. Now go and test it out and see if your directory is now password protected
Some things to note :
- To give additional users access to a directory simply add another line with a username and encrypted password
- Only the password is encrypted, the username is not encryped
htaccess password protect a directory syntax
htaccess password protect a directory code on this is provided for your study purpose, it will guide you to know how to use mod_rewite to create .htaccess files on the apache servers. use it to practice and train your self online
All the tutorials on this site are free, the page code is provifed for you to use it to practice, if you want to help to improve and maintain this work, you can donate to us on.
Htacess tutorial guides withexamples and easy steps to set ip up on your server.
Htaccess force file save
adding new mime types and set timezone
cache control no cache and cache public
gzip page compression
block all bad robots
redirect www to non www
stop directory listing
block and allow referrer
changing home directory page
file access deny control
password protect a directory
hot to create file
php 301 redirect
custom error page
prevent directory listing
set cookie domain authentication
modifying the environment variable