This article explains how to use basic authentication in WordPress.
Edit the .htaccess file
Add the following four lines to the .htaccess file. Write them above the existing settings.
(It is normal to add it at the bottom, but if you do not add it at the top, an error may occur.)
Authname is the name of the authentication. You can name it as you like.
AuthType Basic
AuthName "xxxxxx"
AuthUserFile /var/www/html/.htpasswd
require valid-user
I think the following part was already written.
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
.htpasswd
Place the .htpasswd file in the location previously specified in AuthUserFile (third line from the top).
Once you have placed it, type the following command. (In ubuntu environment)
htpasswd .htpasswd testuser
You will be asked to enter your password, so enter it.
After executing the command, if the following information is added to the empty .htpasswd file, then everything is OK.
testuser:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The part to the left of the colon is your username and the x's are your encrypted password.
If the settings are correct, you should be asked for your basic authentication ID and password when you log in.
Note
If you make a mistake in the settings, when you access it from a browser, you will get an error message saying "File not found."
Check the access log and error log in /var/log/, where error logs are generally generated, to see what kind of errors were generated.
I also experienced an error initially, but by looking at the error.log in /var/log/apache2 I was able to determine that this was because the passwd file was not placed correctly in the location specified in .htaccess.


comment