Use HTAccess to Password Protect Your Web Pages

Vector illustration in flat style of a computer screen with multiple windows open and a red window prompting for a password in front.
  JakeOlimb/Getty Images 

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.
  2. 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.
  1. 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.
  2. 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.
    Format
    mla apa chicago
    Your Citation
    Kyrnin, Jennifer. "Use HTAccess to Password Protect Your Web Pages." ThoughtCo, Dec. 27, 2017, thoughtco.com/password-protect-web-pages-3469335. Kyrnin, Jennifer. (2017, December 27). Use HTAccess to Password Protect Your Web Pages. Retrieved from https://www.thoughtco.com/password-protect-web-pages-3469335 Kyrnin, Jennifer. "Use HTAccess to Password Protect Your Web Pages." ThoughtCo. https://www.thoughtco.com/password-protect-web-pages-3469335 (accessed February 19, 2018).