Password Protecting with htaccess

You might not always have access to cPanel or Plesk to create a protected directory. Although if you do have access to the administration panel creating a protected directory is fairly straightforward, and this article might not be of much use to you. What if you don't have access, but need to create one? This was the case for me recently, I needed to create a protected directory but only had FTP access with Dreamweaver.

First, why might you need to protect a directory? I often create protected directories for sites that are under development. Other times to limit access to certain parts of the website. There are a number of different uses the main reason is anytime you want to restrict access.

To protect a directory you need two things a text file that protects the folder and a text file for your password. First, create the password file. The password file consisents of a username and encrypted password, separated by a colon (:).

For example, username:encryptedpass

How do you get an encrypted password? There are several free web-based utilities out there; see the resource links for more. Once you have the username and password paste that information into a text file and save the file as .htpasswd. Now that you have the file created upload it to the server.

Next, we create the file that protects the directory. Create a file using a text editor and save it as .htacess and paste the following code into that file.

AuthUserFile /full/path/to/.htpasswd
AuthType Basic AuthName
"Restricted Access" Require valid-user

Replace the text "/full/path/to/.htpasswd" with the actual path location of the .htpasswd file on your server. You can also change the text within the quotes to whatever you want to appear in the header of the access box. Once you make the changes save and upload the file. Finally, open a web browser and test to make sure it's working properly.

Protected Directory Prompt
What you should see

Resources

Comments

Leave a comment or suggestion...

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for preventing automated spam submissions.
10 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

About Ishmael


I am a web developer. This is my portfolio. Find out more about me. Contact me about services I can provide.