HTTP auth with .htaccess

Published

Updated

Secure areas with authentication.

HTTP basic auth asks for a username and password before Apache serves a page. On fortrabbit, the dashboard password protection covers a whole environment in one click. `.htaccess` with a `.htpasswd` file protects a single folder or adds more users.

Dashboard or .htaccess

NeedUse
Lock a whole staging environmentDashboard password protection
Protect one folder, such as /admin.htaccess
Several users with their own passwords.htaccess
Protection that follows the code into every environment.htaccess

Both use the same Apache module. While the dashboard password protection is on, Apache does not allow AuthType, AuthUserFile and Require in .htaccess, and a file containing them answers with a 500 error. Use one or the other.

Create the .htpasswd file

The .htpasswd file holds one line per user: the username, a colon and the password hash. Create it locally with the htpasswd tool that ships with Apache:

htpasswd -c .htpasswd john
shell

The tool asks for the password. -c creates the file; leave it out to add a user to an existing file. The result looks like this:

john:$apr1$jw6nGOHx$f/HpoNv9thNMd6w35Ttl80
raw

Store the file outside the root path, so it is never served. With a root path like public or web, put it at the top of the www folder, at /data/www/.htpasswd. When the environment serves www itself, set a root path below it first. Apache never serves files starting with .ht, but a file outside the root path stays safe even when that rule changes.

Create the .htaccess file

Add these lines to the .htaccess of the folder to protect, or to the one in the root path for the whole site:

AuthType Basic
AuthName "Restricted area"
AuthUserFile /data/www/.htpasswd
Require valid-user
apache
.htaccess
LineDoes
AuthType BasicTurns on HTTP basic auth
AuthNameNames the protected area
AuthUserFileAbsolute path to the .htpasswd file
Require valid-userLets in every user from the file

Deploy the files

Commit both files and deploy with git, or upload them with SFTP. A .htpasswd in git stores the password hashes in the repository. To keep them out, upload the file once with SFTP or SSH. The merge deployment strategy leaves it in place; with the replace strategy, add it to the exclude patterns.

Protect only one environment

A .htaccess file in git reaches every environment. To ask for a password only on staging, wrap the lines in a condition on the environment name:

<If "env('FORTRABBIT_ENVIRONMENT_NAME') == 'staging'">
  AuthType Basic
  AuthName "Restricted area"
  AuthUserFile /data/www/.htpasswd
  Require valid-user
</If>
apache
.htaccess

See environment detection for how this works.

Basic auth and the app

Apache checks the password before the request reaches PHP. The browser then sends the credentials in the Authorization header with every request. An app that expects its own Authorization: Bearer token needs that token in a different header while the protection is on.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗