Skip to content

Protecting Website Folders Using .htaccess

The .htaccess file is a powerful Apache configuration tool that allows you to control access to your website’s directories. On ruachost.com, you can use .htaccess to password‑protect folders and prevent unauthorized access.

  • Secure sensitive files (e.g., admin panels, configuration files).
  • Restrict access to private content.
  • Add an extra layer of authentication beyond application logins.
  • Prevent unauthorized users from browsing directories.
  1. Log in via SSH or use a password generator tool.

  2. Create a .htpasswd file outside the public web root (e.g., /home/username/.htpasswd).

  3. Add user credentials in the format:

    username:encryptedpassword

    You can generate encrypted passwords using:

htpasswd -c /home/username/.htpasswd user1

Place the following code in the .htaccess file inside the folder you want to protect:

# Protect Directory
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /home/username/.htpasswd
Require valid-user
  • AuthName → The message shown in the login dialog box.
  • AuthType Basic → Specifies basic authentication.
  • AuthUserFile → Path to the .htpasswd file.
  • Require valid-user → Grants access only to users listed in .htpasswd.
  1. Visit the protected folder in your browser.
  2. A login prompt should appear.
  3. Enter the username and password you created.
  4. If correct, access is granted; otherwise, access is denied.

To protect a specific subdirectory (e.g., /members), update the .htaccess file:

# Protect Subdirectory
AuthName "Members Only"
AuthType Basic
AuthUserFile /home/username/members/.htpasswd
Require valid-user