How to put HTTP authentication to some site or folder

Post Reply
User avatar
myVesta
Site Admin
Posts: 934
Joined: Fri Jun 19, 2020 9:59 am
Has thanked: 8 times
Been thanked: 6 times

Create .htaccess file

First, create a .htaccess file in your public_html to protect the entire site or specific directory, and add the following content in the .htacces file:

Code: Select all

AuthType Basic
AuthName "Please login"
AuthUserFile /home/myuser/web/yourdomain.com/public_html/.htpasswd
require valid-user
If the .htaccess file already exists, add this block at the beginning of the .htaccess file.


Now, let's create the .htpasswd file

The first option is to generate the content of that file online, for example, here - https://www.web2generators.com/apache-t ... -generator
Screenshot_10.png
Screenshot_10.png (26.39 KiB)
Copy-paste the output (you got from that site) to the .htpasswd file, and upload that file to /home/myuser/web/yourdomain.com/public_html/.htpasswd
That's it, you secured a site/folder.

If you don't want an online tool to generate a .htpasswd file, the second option is to generate a .htpasswd file via SSH:

Code: Select all

htpasswd -c /home/myuser/web/yourdomain.com/public_html/.htpasswd myuser
The command will prompt you to enter the desired password.

If you also want to secure static files (like images)

Go to myVesta, into the WEB section, EDIT your domain, and in Proxy Extensions delete everything and leave only, for example, css and js:
Screenshot_9.png
Screenshot_9.png (9.96 KiB)
Save modification.

This way, you will pass all requests (except for css and js) to Apache, which will then request HTTP authentication from the visitors.

Tags:
Post Reply