Plugins Plugin hub Backup SEO Caching Security Forms WooCommerce Images Best backup plugins
Hosting Hosting directory Shared hosting Managed WordPress Cloud hosting VPS hosting By reader location Hosting finder
Themes & builders Compare Rank Math vs Yoast
Tools All tools Hosting finder Plugin stack builder Cost calculator Salt generator Theme detector Who is hosting this? Speed test Headline analyzer robots.txt generator Redirect generator Uptime calculator Blog name generator
Blog Best backup plugins Deals
Guides All guides Speed up WordPress Choose hosting Back up WordPress
How we test

Security · Guide

How to Password-Protect wp-admin

How to Password-Protect wp-admin

Short answer

To password-protect wp-admin, create a .htpasswd file outside the web root with the htpasswd tool, then add AuthType Basic, AuthName, AuthUserFile and Require valid-user to a .htaccess file inside wp-admin. Use it over HTTPS only, and exempt admin-ajax.php so front-end features keep working.

Key takeaways

  • HTTP authentication adds a server-level username and password prompt before the WordPress login screen.
  • The .htpasswd file must sit outside the web root, and the -c flag is used only when creating it.
  • The wp-admin .htaccess file needs AuthType Basic, AuthName, AuthUserFile and Require valid-user.
  • Basic authentication is only safe over HTTPS, since the password travels with every request.
  • Exempt admin-ajax.php or front-end features that use it will break.
  • Nginx does not read .htaccess files, so the same protection goes in the server block instead.

What is HTTP authentication for wp-admin?

HTTP authentication adds a second username and password prompt in front of wp-admin, before the WordPress login screen.

It is a server-level check, not a WordPress feature. A visitor must pass the server prompt before WordPress loads at all, so bots and password-guessing scripts hit the server wall first.

The WordPress hardening guide describes it as a second layer of protection around the admin area, the login screen and your files.

This is an extra layer, not a replacement for the basics. It does not replace strong WordPress passwords, two-factor authentication, updates or backups. It sits in front of them and cuts down the number of login attempts that ever reach WordPress.

How does HTTP authentication work?

The server answers the first request with a 401 status, the browser asks for a username and password, and the server checks them against a password file.

With Basic authentication, the browser sends the username and password with every request, encoded in Base64, according to the Apache documentation on authentication. Base64 is an encoding, not encryption, so the password can be read if the connection is not encrypted.

The Apache documentation notes that if data really needs to be secure, you should use mod_ssl as well, which means this method belongs on HTTPS sites only.

If the password is wrong, the server answers with a 403 status instead, according to the Akeeba documentation on protecting wp-admin.

A correct password lets the request through to the WordPress login page, where the normal WordPress login still applies. The two passwords are separate, so use a different username and password for the server prompt than for WordPress.

What do you need before you start?

You need an Apache or LiteSpeed server, HTTPS on the site, and a way to edit files on the server.

HTTP authentication with .htaccess works on Apache and LiteSpeed, but not on Nginx-only servers, according to the Akeeba documentation on protecting wp-admin. If your host uses Nginx, skip to the Nginx section below instead. Check with your host if you are not sure which server software your site runs on.

Back up the current .htaccess file before you change anything. The WordPress hardening guide warns that code placed between the # BEGIN WordPress and # END WordPress markers can be overwritten by WordPress, so keep your changes outside those markers.

You also need the full server path to your files, since the rules point at the password file by its full path, not a web address.

How do you create the .htpasswd file?

Run the Apache htpasswd tool over SSH, or use your hosting panel, to create a password file outside the web root.

The password file holds one user:hashed-password pair per line, and it should sit somewhere that cannot be reached from the web.

The Apache documentation on authentication shows putting it in a folder such as /usr/local/apache/passwd when documents are served from /usr/local/apache/htdocs. If your site lives in public_html, put the file one level above it, in your home folder.

Create the file with the first user like this:

htpasswd -c /home/username/.htpasswd adminname

The htpasswd tool asks for the password twice, according to the Apache documentation. The -c flag creates the file, so use it only the first time.

To add a second user, run the same command without -c, or the existing entries will be wiped out and replaced. Give each admin their own username, so you can remove one person without changing the password for everyone else.

If your host has no SSH access, use the password-protect option in your hosting panel, or a file manager to upload a .htpasswd file you made on your own computer. Never store a plain-text password in the file: the tool writes a hash, and the server checks the password against that hash.

How do you add the rules to wp-admin?

Add four lines to a .htaccess file inside the wp-admin folder, pointing at the password file.

The Apache documentation on authentication gives this pattern for protecting a folder:

AuthType Basic
AuthName "WordPress administration"
AuthUserFile /home/username/.htpasswd
Require valid-user

AuthType Basic picks the authentication method, and AuthName sets the label the browser shows in the password prompt. AuthUserFile gives the full server path to the password file you created. Require valid-user lets in any user listed in that file, while Require user adminname would allow only one named user.

Your server must allow these rules in .htaccess files. The Apache documentation states that you need an AllowOverride AuthConfig setting, and that the mod_authn_core and mod_authz_core modules must be loaded.

On shared hosting, the host normally sets this up already, but some hosts block it, so ask your host if the prompt never appears. Test in a private browser window after saving, and keep a second way into the server, such as SFTP, in case you lock yourself out.

What about Nginx sites?

Nginx does not read .htaccess files, so the same protection goes in the Nginx server block instead.

The Akeeba documentation on protecting wp-admin states that .htaccess files are only compatible with Apache, LiteSpeed and a few other servers, and that Nginx is incompatible.

On Nginx you add an auth_basic block to the server configuration for the wp-admin location, with an auth_basic_user_file pointing at the same kind of password file.

You need server access to edit that file, so on managed Nginx hosting, ask the host to add it or use the host panel if it offers the option.

If you cannot change the server configuration, use another layer instead. A web application firewall can add a login wall or block bad login traffic before it reaches WordPress, as explained in how to set up a web application firewall for WordPress.

Two-factor authentication also adds a second check at the WordPress login, as shown in how to add two-factor authentication to WordPress.

How do you keep admin-ajax.php working?

Exempt admin-ajax.php from the password prompt, or front-end features that use it will break.

The WordPress hardening guide warns that securing the wp-admin folder can break WordPress features such as the AJAX handler at wp-admin/admin-ajax.php. Themes and plugins use that file for live search, forms, cart updates and editor autosaves, and those requests come from visitors who have no server password.

Add a rule inside the wp-admin .htaccess file that lets admin-ajax.php through without the prompt, before the Require valid-user line takes effect. The exact syntax depends on your Apache version, so test it right after you save.

Open the site as a logged-out visitor, submit a form or use a feature that loads content without a page refresh, and confirm it works. If a feature stops working after you add the prompt, the exemption is the first thing to check.

MethodWhere it runsBest fitMain risk
HTTP Basic authenticationWeb server, before WordPressSmall teams on Apache or LiteSpeedBreaks admin-ajax.php if not exempted
IP allowlistWeb server, before WordPressAdmins with a fixed office or VPN addressLockout when your address changes
Two-factor authenticationWordPress login screenEvery site, any serverLost second factor needs a recovery path
Web application firewallIn front of the serverSites under heavy bot trafficNeeds correct rules and upkeep

How do you test the protection and fix a lockout?

Test from a private window, check the front end still works, and remove the rules by SFTP if you get locked out.

Open /wp-admin/ in a private browser window and confirm the server prompt appears before the WordPress login. Log in with the server username and password, then with the WordPress login, and confirm the dashboard loads.

Then open the front end as a logged-out visitor and check that pages, forms and the cart still work. If the prompt appears on the front end too, the rules are in the wrong folder: they belong in the wp-admin folder, not the site root.

If you get locked out, connect by SFTP or the hosting file manager and delete or rename the .htaccess file inside wp-admin. That removes the prompt at once.

The same fix applies after moving a site: the Akeeba documentation notes that restoring a site on a Windows server can return a blank page or a 500 error until you remove the .htaccess and .htpasswd files. For other login problems, read how to fix being locked out of wp-admin.

What are the limits of this method?

HTTP Basic authentication is a thin extra wall, and it has real limits you should know about.

First, the password travels with every request, so it is only safe over HTTPS. If your site still runs on plain HTTP, set up a certificate first by following how to install an SSL certificate on WordPress.

Second, it does not stop every attack: the WordPress hardening guide notes that most attacks are either crafted requests aimed at old plugins, or brute-force password guessing, and this prompt only slows the second kind.

Third, it adds friction for real users. Every admin, editor and plugin that calls the admin area must pass the prompt, and some tools, such as remote publishing apps or uptime checks that hit admin pages, may fail.

For that reason, keep the number of server-level users small, use a different password from WordPress, and pair the prompt with the other layers in how to stop brute-force attacks on WordPress.

Used that way, a second password prompt turns a public login page into a door that most bots never get to knock on.

Frequently asked questions

Does HTTP authentication work on any host?

Yes, on Apache and LiteSpeed servers, where .htaccess files work. On Nginx-only servers the same protection goes in the server block instead, since Nginx does not read .htaccess files. If you cannot change the server setup, use a firewall or two-factor authentication as the extra layer.

Does the server password replace my WordPress password?

No. HTTP authentication is a second prompt in front of the WordPress login, not a replacement for it. Visitors still need a WordPress username and password after passing the server prompt, and the two passwords should be different.

What do I do if I lock myself out?

Connect by SFTP or your hosting file manager and delete or rename the .htaccess file inside the wp-admin folder. That removes the prompt at once. Keep a second way into the server, such as SFTP, before you add the rules.

Will password protection break admin-ajax.php?

Yes, unless you exempt it. Themes and plugins use admin-ajax.php for live search, forms, cart updates and autosaves, and those requests come from visitors with no server password. Add an exemption for that file and test a form right after you save.

Is HTTP Basic authentication safe without HTTPS?

Basic authentication sends the username and password with every request, encoded in Base64, which is not encryption. That is why it belongs on HTTPS sites only. Set up a certificate first if your site still runs on plain HTTP.

Is it worth adding if I already have two-factor authentication?

It stops many automated login attempts from ever reaching WordPress, which cuts bot traffic and failed login noise. It does not fix old plugins, weak WordPress passwords or missing updates, so pair it with two-factor authentication, updates and backups.

Sources

  1. Hardening WordPress WordPress Developer Resources
  2. Authentication and Authorization Apache HTTP Server
  3. Protect Your WordPress Administration With a Password Akeeba Ltd
  4. Roles and Capabilities WordPress.org

Is your product missing, or something out of date?

Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.

About the editors

Written and checked byWPLegible Editorial Team Independent WordPress reviews and guides

Researched from the primary sources listed above and checked against our editorial rules before publishing.