Hello,
I am using Web Expression and would like to restrict access to a part of my site to people who have a password.
What are the ways to achieve this?
Thank you for your insight.
Hello @LeonP
Put the entire part of the site that needs to be protected into a subdirectory of the site and follow the documentation to protect this subdirectory with htaccess/htpasswd
https://docs.ovhcloud.com/fr/guides/web-cloud/web-hosting/htaccess-protect-directory-by-password
Hi @LeonP
The safest and standard way to restrict access to a part of your site is to use Apache basic authentication via the .htaccess and .htpasswd files.
Follow these steps:
Connect to your hosting via FTP and go to the root folder of your site. There you need to create these three files:
crypt.php: A temporary script you will use to generate the hashed password..htpasswd: The file that will store the username and the hashed password. You should create it inside the folder you want to protect (e.g., /admin)..htaccess: The file that tells the server to request a password. You should create it in the same folder as the previous one.
Let's generate the hashed password.
Open the crypt.php file you created in the root and paste this code, replacing tu_contraseña with the password you want to use:
<?php
$string = password_hash("tu_contraseña", PASSWORD_BCRYPT);
echo nl2br("$string");
?>
Save the file and access it from your browser (for example, tudominio.com/crypt.php). The page will show a long string of text. Copy that string, as it is your hashed password.
Let's configure our .htpasswd file.
Open the .htpasswd file you created in the folder to protect and paste the following, replacing tu_usuario and la_cadena_copiada with your data:
tu_usuario:tu_contraseña
Finally, let's configure your .htaccess file.
Open the .htaccess file in the same folder and paste these lines. It is important that in AuthUserFile you use the absolute path from the server to your .htpasswd file. You can see the exact path in your OVH client area, under the FTP‑SSH section.
AuthName "Acceso restringido"
AuthType Basic
AuthUserFile /home/tu_usuario_ftp/www/admin/.htpasswd
Require valid-user
Note: Once everything works, delete the crypt.php file from your server for security.