How to Protect Your WordPress Admin Area Using .htaccess
Lock down wp admin before bots ever hit your login
WordPress admin .htaccess rules give you an extra security layer before attackers even reach your login form. By restricting access to /wp-admin/ and wp-login.php at the web server level, bots and password-spraying scripts hit a wall instead of your dashboard. In this tutorial you will add practical .htaccess protections that make brute-force attacks far harder without relying entirely on plugins.
You will first locate and back up your .htaccess file, then allowlist trusted IP addresses for the login page, add a password prompt in front of the admin area, and finish with a few extra hardening rules. Follow each step carefully and you will end up with a tougher WordPress admin area that still works smoothly for you and your team.
What You Need to Start
- Access to your hosting control panel and its File Manager, or an FTP/SEO and UX, and where you will see it in daily work.”>SFTP client.
- Ability to view and edit hidden files (such as
.htaccess) in File Manager or your FTP client. - A recent full-site backup, or at least a backup of your WordPress root directory.
- Your current public IP address so you can allowlist it for
wp-login.php. - A WordPress site running on Apache or LiteSpeed, where
.htaccessrules are supported.
Step 1: Back up and locate .htaccess
The .htaccess file is parsed before WordPress runs, so a typo here can break the entire site. Before you paste any security rules, create a backup and confirm exactly where your active .htaccess file lives.
Most WordPress sites store .htaccess in the same folder as wp-config.php. If you are not sure, always look for that file first and treat its folder as your WordPress root.
- Log into your hosting control panel and open the File Manager or File Manager app.
- In File Manager, open your WordPress site folder, usually
public_htmlor the directory that containswp-config.php, and enable the option to show hidden files so.htaccessappears. - If you do not see
.htaccess, create it by clicking New File and naming it.htaccessin the WordPress root directory. - Right click
.htaccessand choose Download to save a copy locally, then optionally duplicate it in File Manager as.htaccess-backup.
.htaccess causes a 500 error, immediately rename the file to something like .htaccess-broken using File Manager or FTP. Apache will ignore the renamed file and your site should load again while you restore the backup. If your site was already using pretty permalinks, you will usually see a default WordPress block inside .htaccess like this:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress Keep this block intact. You will add your admin protection rules either above or below it, not inside it.
Step 2: Restrict wp login by IP address
One of the strongest protections you can add is to allow only specific IP addresses to access wp-login.php. Automated attacks from other addresses will never even see your login form.
This method works best if your internet connection uses a static or rarely changing IP address. If your IP changes often, you can still use this rule, but you must be comfortable updating the allowlisted addresses whenever you lose access.
- In File Manager, right-click
.htaccessin your WordPress root and choose Edit or Code Editor to open it. - Above the
# BEGIN WordPressline, paste the following snippet, then replace the example IP addresses with your own public IPs.
# Restrict access to wp-login.php by IP <Files wp-login.php> Require all denied Require ip 203.0.113.10 Require ip 198.51.100.25 </Files> - Replace
203.0.113.10and198.51.100.25with the real IP addresses you want to allow, one perRequire ipline. - Click Save in the editor and close it.
- From a browser on an allowlisted connection, visit
https://yourdomain.com/wp-login.phpand confirm the login page loads normally. - From a different network or mobile connection that is not allowlisted, try to open
wp-login.phpand confirm you see a 403 Forbidden or similar error instead of the login form.
Step 3: Password protect wp admin folder
HTTP Basic Authentication adds a second username and password in front of /wp-admin/. Even if someone guesses a WordPress password, they still cannot reach the dashboard without first passing this web server level prompt.
To make this work, you will create a .htpasswd file that stores an extra login and then tell Apache to require those credentials whenever anyone loads the admin folder.
- In File Manager, open the
wp-adminfolder inside your WordPress installation. - If there is no
.htaccessfile inwp-admin, create one by clicking New File and naming it.htaccessinside thewp-adminfolder. - Navigate one level above your web root (for example,
/home/username/) and create a new file named.htpasswd. Place it outside any publicly accessible web directory for safety. - Generate a username and encrypted password using your hosting provider’s password protection tool or a command-line utility, then paste the resulting line (for example,
adminuser:$apr1$...) into.htpasswdand save the file. - Return to
wp-admin, edit its.htaccessfile, and add the password protection snippet below, replacing theAuthUserFilepath with the absolute server path to your.htpasswdfile.
# Password protect wp-admin AuthType Basic AuthName "Restricted Area" AuthUserFile /full/server/path/to/.htpasswd Require valid-user - Save the file, then visit
https://yourdomain.com/wp-admin/. Confirm the browser now shows an extra username and password prompt before the usual WordPress login page.
If you cannot determine the correct absolute path for AuthUserFile, open a support ticket with your host and ask them for the full server path to the folder that contains your .htpasswd file.
Step 4: Add extra .htaccess hardening rules
With your login script and admin directory protected, you can safely add a few more .htaccess rules that protect sensitive files and reduce the attack surface around your dashboard.
The following examples are generally safe on most shared hosts, but you should always test them on a staging site or during a quiet traffic period if possible.
- Edit your main WordPress root
.htaccessfile again and place the extra rules above the# BEGIN WordPressblock.
# Disable directory browsing Options -Indexes # Protect wp-config.php <Files wp-config.php> Require all denied </Files> # Block xmlrpc.php if you do not need it <Files xmlrpc.php> Require all denied </Files> - Click Save in the editor and reload a few public pages on your site to confirm they still work as expected.
- If you rely on services that use
xmlrpc.php(such as some mobile apps or remote publishing tools), remove or comment out thexmlrpc.phpblock and save again.
For more ways to harden WordPress beyond .htaccess rules, review the official Hardening WordPress guide from WordPress.org.
Step 5: Test and roll back changes
After every .htaccess change, you must test both the happy path for trusted users and the blocked path for unwanted traffic. Proper testing ensures you lock out attackers without accidentally locking out yourself or your team.
Use multiple devices and networks where possible, and keep an emergency way to revert changes open the whole time.
- Open a new private or incognito browser window and sign in from an allowlisted IP to confirm you can still reach
wp-login.phpand/wp-admin/after entering the new HTTP authentication credentials. - From a different network that is not allowlisted, try visiting
wp-login.phpand/wp-admin/. Confirm you see a forbidden or unauthorized message and cannot see the login form itself. - Clear any caching layers you use, including browser cache, WordPress caching plugins, and server-level caching from your host.
- If you encounter a white screen or 500 error, rename
.htaccessvia File Manager or FTP, reload the site, and then restore the backup version you downloaded earlier. - Once everything works, document the rules you added and where you placed them so future you (or your developer) can understand the security setup.
If you want a deeper understanding of how .htaccess and Apache work together, you can read the official Apache .htaccess documentation for more background.
Conclusion You Are Ready to Go
By carefully backing up and editing your .htaccess file, you have added several powerful protections in front of your WordPress admin area. Your login script is now available only from approved IP addresses, your wp-admin folder has its own HTTP password gate, and sensitive files around your dashboard are shielded from direct access.
These changes make automated attacks and casual probing much harder, especially when combined with strong passwords, up-to-date plugins, and regular backups. Keep your IP allowlist and .htpasswd details somewhere safe, schedule periodic tests of the login flow, and you will enjoy a noticeably more resilient WordPress admin experience.
Further Reading
- Install WordPress step by step
- WordPress migration checklist for blogs
- How to edit WordPress files
- WordPress migration checklist for blogs
- Beginner WordPress security best practices guide
Frequently Asked Questions
Do these .htaccess rules work on Nginx hosting
.htaccess files at all. If your site runs on Nginx, you must add equivalent rules directly to the server configuration, which is handled by your hosting provider or server administrator. Always confirm with your host which web server your plan uses before editing or relying on .htaccess changes. What if my IP address keeps changing
Can I lock myself out of wp admin with these rules
AuthUserFile path. This is why you must always keep File Manager or FTP access working in a separate browser tab. If you lose access, rename .htaccess or remove the new rules, reload the site, and then correct the configuration before testing again. Is .htaccess protection enough on its own
Where should I place these rules inside .htaccess
# BEGIN WordPress block, never inside it. WordPress may regenerate that section when you change permalinks or certain settings, which could overwrite anything placed between # BEGIN WordPress and # END WordPress. Keeping your rules outside this block ensures they remain intact. How do I undo these .htaccess changes later
.htaccess in your WordPress root and remove the security snippets you added, then save the file. For the password protection, also remove or rename the .htaccess in wp-admin and, if desired, delete the .htpasswd file. Always keep at least one known-good backup of .htaccess so you can restore it instantly if you make a mistake.




