Saturday 8 March 2014

With .htaccess Password protect your Wordpress site Directory

I this tutorial I will show you how to password protect your wordpress or any site directory with .htaccess 



Before we proceed a little about information about http basic authentication from wikipedia


HTTP Basic authentication (BA) implementation is the simplest technique for enforcing access controls to web resources because it doesn't require cookiessession identifier and login pages. Rather, HTTP Basic authentication uses static, standard HTTP headers which means that no handshakes have to be done in anticipation
The BA mechanism provides no confidentiality protection for the transmitted credentials. They are merely encoded with Base64 in transit, but not encrypted or hashed in any way. Basic Authentication is, therefore, typically used over HTTPS.
Because the BA header has to be sent with each HTTP request, the web browser needs to cache credentials for a reasonable period of time to avoid constantly prompting the user for their username and password. Caching policy differs between browsers. Microsoft Internet Explorer by default caches them for 15 minutes.
While HTTP does not provide a method for web server to instruct the browser to "log out" the user (forget cached credentials), there are a number of workarounds using specific features in various browsers. One of them is redirecting the user to an URL on the same domain containing credentials that are intentionally incorrect



For this we will need two files .htaccess and .htpasswd 

.htaccess will contain the information about the path of .htpasswd and a message that will be shown when any will try to access the protected directory. In the above image you can see the message 'Restricted Zone' you can set it to anything you want, it doesn't really matter.


Now open your file manager in Cpanel and Create a new file named .htaccess

Now Open .htaccess in code editor add the code given below and save it.

Else if you want to do it using ftp create a file named htaccess.txt open in notepad add the code given below and later rename it to .htaccess

Add the following code in it 

ErrorDocument 401 "Unauthorized"
AuthType Basic
AuthName "Restricted ZOne"
AuthUserFile /home/yourcpanelusername/.htpasswds/.htpasswd
Require valid-user


Now we will have to similarly create the file .htpasswd to make the process simple and easily I have marked point 1 2 3 4

We are storing the .htpasswd file in the .htpasswds folder because the folder is not accessible by public this is important because there is no reason we would like to share the .htpasswd to the public

Click where 4 is marked it will open the .htpasswds folder. Now in this folder create a new file .htpasswd and 

Add your username:FIee9ppkFJh1U (the username here is in simple form but the password is encrypted in base 64 encoded string after decryption it will be 'password' )

To generate the encrypted password we will use this simple online tool Just type username, your desired password and click submit.

After submitting it will generate the encrypted password just copy it and paste in your .htpasswd file.

Remember to login you will need the original not encrypted password.


No comments:

Post a Comment