Skip to content

Adding a basic authentication to custom folders

Sometimes, it is useful to add a basic authentication to a specific folder. This can be done by adding a .htaccess file to the folder. The .htaccess file should contain the following content:

AuthType Basic
AuthName "Restricted Content"
AuthUserFile /path/to/.htpasswd
Require valid-user

Attention: The path to the .htpasswd file must be an absolute path. The .htpasswd file should contain the username and password in the following format:

username:password

To create the .htpasswd file, you can use one of the following commands:

htpasswd -c .htpasswd username
htpasswd -b -c .htpasswd  username password

Apache adjustments for folder access

By default, the .htaccess file redirects all requests to the index.php file. If you want to allow access to a specific folder, you can add the following condition to the last redirect rule to prevent the redirection for the specific folder:

    # synQup adjustment: Temporarily, for the time being, we allow directories under public/documentation to be served
    # as well. The first RewriteCond is added by us (all other RewriteCond conditions are brought by symfony).
    # This is to allow the documentation to be served without the need for a separate vhost.
    # The documentation folder is a symlink to /opt/synqup-media which is a persistent volume. It is recommended to
    # create a basic authentication for this folder to prevent unauthorized access. See the guide in MKDocs synQup
    # Core Documentation how to create a .htaccess and .htpasswd.
    RewriteCond %{REQUEST_URI} !^/documentation
    RewriteCond %{REQUEST_URI} !^/index.html$
    RewriteCond %{REQUEST_URI} !^/$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]

This line is new:

    RewriteCond %{REQUEST_URI} !^/documentation

This line prevents the redirection for the /documentation folder. You can add more folders by adding more RewriteCond lines.