How to password-protect web pages with .htpasswd

How to password-protect web pages with .htpasswd

This article shows how Nexcess clients can use FTP and SSH to restrict access to web pages to one or more users.

But first...

You need both FTP and SSH access to the server hosting your website. See How to add FTP accounts with SiteWorx and How to enable SSH access for more information.


Attention: Any input in brackets ([username][], and so on) represents a placeholder. Replace the brackets and everything between them with the indicated information. 

  1. Log in using FTP and download your .htaccess file from the folder you wish to protect; for example: 

    /home/username/ If this file is not here, you may need to create it.

  2. At the top of the .htaccess file, insert the following:

    Attention: The AuthUserFile path should be the target location for storing your username and password.  We recommend doing this in a location outside of the html folder.

    AuthType Basic
    AuthName "Password Protection"
    AuthUserFile /chroot/home/[username]/[]/.htpasswd
    Require valid-user

    If you wish to block a specific page, you would use <FilesMatch> to limit this to a single page, like so:

    AuthType Basic
    AuthName "Password Protection"

    AuthBasicProvider file
    AuthUserFile /chroot/home/[username]/[]/.htpasswd
    <FilesMatch “[secret.php]”>
    Require valid-user

  3. Log in with SSH, then use the cd command to change your directory to the target directory for the .htpasswd file:

    cd /chroot/home/[username]/[]/
  1. To generate a new .htpasswd file with one user, issue:

    htpasswd -nb [username] [password] > .htpasswd

    After this, you can use htpasswd -nb command to generate new username and password combinations to add to the .htpasswd file. For example:

    htpasswd -nb [username2] [password2]

  1. The output will resemble the following; copy it to your clipboard:


  1. Using FTP, add the new user as a new line to the existing .htpasswd file. Repeat Steps 5 and 6 for every user as necessary. For example:


For 24-hour assistance any day of the year, contact our Support Team by email or through the Client Portal.

Article Rating (1 Votes)
Rate this article
  • Icon PDFExport to PDF
  • Icon MS-WordExport to MS Word
Attachments Attachments
There are no attachments for this article.
Related Articles RSS Feed
How to secure your WordPress site
Added on Wed, Dec 26, 2018
How to activate two-factor authentication in SiteWorx
Added on Fri, Dec 14, 2018
How to create SSH keys in macOS and Linux
Added on Mon, Jun 11, 2018
How to improve the security of your Magento store
Added on Wed, Dec 26, 2018
How to secure your ExpressionEngine website
Added on Mon, Jan 14, 2019
How to transfer files to a server with SFTP
Added on Wed, May 23, 2018
How can I prevent my site from being compromised?
Added on Mon, Mar 18, 2019
How to add SSH keys to your Nexcess Cloud account
Added on Tue, Oct 23, 2018
How to enable SSH access
Added on Mon, Dec 17, 2018
How to install OpenVPN
Added on Wed, Dec 26, 2018