Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Password Protect Your WordPress Admin (wp-admin) Directory

Last updated on by
Follow WPBeginner on YouTube
How to Password Protect Your WordPress Admin (wp-admin) Directory

As you read the title, you are probably wondering isn’t the wp-admin directory already password protected. You are required to login right. Well that is true, but to add an additional layer of security popular sites often add an extra layer of authentication. Few days ago, we started seeing some suspicious activity on WPBeginner, so our host HostGator advised us to password protect our WordPress admin directory. Apparently popular sites like Mashable do the same. In this article, we will show you a step by step guide on how to password protect your WordPress admin (wp-admin) directory.

To keep things easy and simple, we will only cover cPanel web hosting companies here just because cPanel has an easy enough interface to add password protected directories.

Login to your cPanel. Scroll down till you see the Security Tab. Click on the “Password Protect Directories” icon.

Password Protect Directories

When you click on that, a lightbox popup will show up asking for directory location. Just click on web root. Once you are there, navigate to the folder where your WordPress is hosted. Then click on the /wp-admin/ folder. You will see a screen like this:

Security Settings for a Folder

Simply check the box to password protect the directory. Then create a user for the directory. That is it. Now when you try to access your wp-admin directory, you should see an authentication required box like this:

Authentication Required

Manual Method

First create a .htpasswds file. You can do so easily by using this generator. Upload this file outside your /public_html/ directory. A good path would be:

home/user/.htpasswds/public_html/wp-admin/passwd/

Then, create a .htaccess file and upload it in /wp-admin/ directory. Then add the following codes in there:

AuthName "Admins Only"
AuthUserFile /home/yourdirectory/.htpasswds/public_html/wp-admin/passwd
AuthGroupFile /dev/null
AuthType basic
require user putyourusernamehere

You must update your username in there. Also don’t forget to update the AuthUserFile location path.

I have a 404 Error or a Too many redirects error

Well this can happen depending on how your server is configured. To fix this issue, open your main WordPress .htaccess file and add the following code there before the WordPress rules start.

ErrorDocument 401 default

Well there you have it. Now you have double authentication for your WordPress admin area. This is a good alternative to limiting wp-admin access by IP address.

Update: Here is how to fix the Admin Ajax Issue

If you password protect your WordPress Admin directory, then it will break the Ajax functionality in the front-end (if it is being used). In our case, we don’t have any plugins that is using ajax in the front-end. But if you do, then here is how you fix that issue.

Open the .htaccess file located in your /wp-admin/ folder (This is NOT the main .htaccess file that we edited above).

In the wp-admin .htaccess file, paste the following code:

<Files admin-ajax.php>
    Order allow,deny
    Allow from all
    Satisfy any 
</Files>

Source: Sivel


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Daniel Papenfuß says:

    Hey Bro,

    your hint for the ajax file helped me a lot – thx for that :)

    Bye,
    Daniel

  2. ajakayetolu says:

    I recently install wordpress but mydomain/wp-admin/ keeps asking for username and password despite inputing the correct password and username its just as if it is refreshing itself.

  3. 【Ƿ】 Fran Kee says:

    Good Tutorial. While admin-ajax.php might need extra enabling,
    there is one file on one level up (WP root level), that imho deserves extra disabling:

    require user

    Unless you truly have a blog where regular readers obtain WP accounts for commenting or such, this is not necessary. Extra-htpasswd-protecting this makes brute-forcing of WP accounts harder…

  4. Jeff says:

    Thanks! You just saved me a lot of time with that AJAX fix. I was looking everywhere and couldn’t figure out the problem!

  5. dylanatstrumble says:

    Hi,

    I have tried putting the ErrorDocument 401 default in many places in the main .htaccess file to no avail

    I am running W3 Total Cache and the first chunk of the code relates to that

    I have put it before the W3 code which results in a 500, I have tried putting it after the W3 code # END W3TC just before the # BEGIN WordPress

    I get a 500 there as well

    If only this would work, I would be a happy camper

    Hosted by Go Daddy and creating the protected folder via CPanel

    I am putting off calling Go Daddy for the fourth time. as on this particular issue, they have not been at their best

    Thanks in advance

  6. WPBeginner Staff says:

    Are you certain that you are entering the correct username and password?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.