Password Protect Your Web Pages

Use HTAccess to Password Protect Your Web Pages

Password protect your Web pages
Password protection prompts generally show up before you access the page. This is an example of a password protection prompt coming from the server via htaccess. Screen shot by J Kyrnin

What is Password Protection

There are many websites that cause a box to pop up asking you for a username and password. If you don't know the password, you can't enter the site. This provides some security to your Web pages and gives you the chance to choose who you want to allow to see and read your Web pages. There are many ways to password protect your Web pages, from PHP, to JavaScript, to HTAccess (on the Web server).

When Should You Password Protect Pages?

With HTAccess, you can password protect any page or directory on your Web server. You can even protect the entire website if you want to. HTAccess is the most secure method of password protection, as it relies on the Web server, so the valid usernames and passwords are never shared with the Web browser or stored in the HTML like they can be with other scripts. People use password protection:

  • Hiding new versions of your website from the public until they are ready to launch.
  • Protecting private sections of your website so that only people you know and trust can read them.
  • Providing paid content to your customers and only allowing access via a password.
  • Creating a private forum for select readers.

It's Easy to Password Protect Your Web Pages

You need to do two things:

  1. Create a password file to store the usernames and passwords that will have access to the directory.
  1. Create an htaccess file in the directory to be password protected.

Create the Password File

  1. Open a new text file called
    .htpasswd
    Note the period at the beginning of the filename.
  2. Use a password encryption program to create your passwords. Paste the lines into your .htpasswd file and save the file. You will have one line for every username that requires access.
  1. Upload the .htpasswd file to a directory on your Web server that is not live on the Web. In other words, you should not be able to go to http://YOUR_URL/.htpasswd - it should be in a home directory or other location that is secure.

Create the htaccess File

  1. Open a text file called
    .htaccess
    Note the period at the beginning of the filename.
  2. Add the following to the file:
     AuthUserFile /path/to/htpasswd/file/.htpasswd
     AuthGroupFile /dev/null
     AuthName "Name of Area"
     AuthType Basic
     require valid-user
     
  3. Change
    /path/to/htpasswd/file/.htpasswd
    to the full path to the .htpasswd file you uploaded in above.
  4. Change
    "Name of Area"
    to the name of the site section being protected. This is used primarily when you have multiple areas with different protection levels.
  5. Save the file and upload it to the directory you want protected.
  6. Test that the password works by accessing the URL. If your password doesn't work, go back to the encryption programs and encrypt it again. Remember that the username and password will be case-sensitive. If you are not prompted for a password, contact your system administrator to make sure that HTAccess is turned on for your site.