Browse - Computer Tips - How can I password protect part of a website using an .htaccess file?
Q. How can I password protect part of a website using an .htaccess file?
A. First make sure .htaccess files are enabled in your Apache configuration.
On a Linux system this will be somewhere like
This means in folder /htdocs and below .htaccess files will be read
The module needs to be loaded too. But it is by default.
And there is an Apache directive to stop web users from viewing files that begin with ".ht" but that is also enabled by default.
In the folder you want to protect make a file called .htaccess containing:
<LIMIT GET POST>
require user myuser
It doesn't have to be exactly this way but this gets you going.
The says only user myuser with the correct password from /etc/httpd/conf/htpasswd is allowed in. I am not putting a dot in front of htpasswd because there is no need to hide it since its out of the website.
Use the htpasswd command to make that file:
htpasswd -c /etc/httpd/conf/htpasswd myuser
You will be prompted for the password.
Restart Apache and test it.
There are other ways, besides using an .htaccess file.