How to Block IP Addresses in WordPress Using htaccess
In this guide
When You Should Block IP Address WordPress Visitors
To block IP address WordPress traffic from a specific source, you first need a good reason. The usual reasons are repeated brute force login attempts, comment spam from the same address, content scraping that hammers your server, or a flood of fake requests that slow your site down for real visitors.
Blocking is a targeted tool, not a general one. It works best against a single address or a small range that keeps causing trouble, and it works poorly against large botnets that attack from thousands of different addresses. Before you block anything, check your logs or your security plugin to confirm that the trouble really comes from one place.
If the abuse comes from many addresses at once, a firewall or a content delivery network with bot protection will serve you better than a long list of blocked addresses. Use htaccess blocking for the stubborn repeat offenders, and leave the large-scale attacks to tools built for them.
How to Find the IP Address You Need to Block
Your first job is to identify the offending address accurately. Your security plugin usually shows a list of failed login attempts with the address attached, and your hosting control panel or raw server logs show which addresses request your pages most often. Look for patterns: hundreds of requests for your login page, or the same address posting spam comments again and again.
Be careful with shared addresses. Many visitors reach your site through a shared network, a mobile carrier or a workplace connection, so one address can represent many innocent people. Blocking an address that belongs to a large network can lock out genuine readers, so check whether the address belongs to a hosting company or a data centre before you block it, because those are far more likely to be bots than readers.
Write down the address exactly, including every number and dot. A single wrong digit can block the wrong person, or fail to block anyone at all, and typos in a server file can cause bigger problems than the attack you are trying to stop.
How to Block IP Address WordPress Traffic with htaccess
To block IP address WordPress requests at the server level, you edit the htaccess file in your site's root folder. Open it through your hosting file manager or an FTP client, and add a rule that denies access from the address you identified. The rule names the address and tells the server to refuse it, while everyone else continues to reach your site normally.
Add one rule per address, and keep a comment beside each one noting the date and the reason, so that future you can understand why it is there. Save the file and test your site immediately from a normal connection to confirm that it still loads. If the site breaks, remove the lines you just added and try again more carefully.
Always keep a backup copy of the htaccess file before you edit it. Download it to your computer first, so that you can restore it in seconds if a mistake takes your site down. A file that controls how your server behaves deserves that small precaution every single time.
What to Do When Blocking One Address Is Not Enough
Attackers who notice they are blocked often return from a different address, so a single block is rarely the end of the story. Watch your logs after you block an address and be ready to add more, but do not let the list grow forever. A very long block list slows down your server slightly on every request, and it becomes hard to manage.
Consider blocking a range of addresses when the abuse clearly comes from one network, but be cautious: a range can include innocent users too. A narrow range from a hosting provider that has no real readers is usually safe, while a wide range from a mobile network is not.
For persistent attacks, move the defence up a level. A security plugin that limits login attempts, a firewall that filters bad traffic before it reaches your server, and a content delivery network that absorbs floods will all do more than a manual block list ever can. Use htaccess blocking as one layer among several, not as your only defence.
Avoid Locking Yourself Out of Your Own Site
The most common mistake when editing htaccess is blocking your own address by accident. Always check your own address before you start, and never add a rule that matches it. If you work from a connection whose address changes often, be extra careful, because an address that is yours today may belong to someone else tomorrow.
If you do lock yourself out, do not panic. Log in to your hosting control panel or FTP, open the htaccess file, and remove the offending lines. That is another reason to keep a backup copy of the file: recovery takes a minute when you have one and far longer when you do not.
Review your block list every few months and remove addresses that have been quiet for a long time. Old blocks can accidentally catch real visitors whose addresses have been reassigned, and a clean list is easier to understand and safer to maintain than a list full of entries nobody remembers.
Earn
Zohaib Faisal is the founder and editor of EarnDesk. Based in Lahore, Pakistan, he runs the online store Haseeb Collections and works as a freelancer on Fiverr (virtual assistance, AI content writing, AI prompt engineering and digital marketing). He builds websites — Haseeb Collections, EarnDesk and a temporary-email service — writes SEO-focused content, and has hands-on AdSense experience across his sites. He has earned 15+ professional certificates from Google, IBM, Microsoft, HubSpot, Cisco, the Linux Foundation, Unity, Semrush and Harvard (CS50P), and creates the YouTube channel Clips by Zohaib. On EarnDesk he writes practical, honest guides to help beginners earn online — without hype or "get rich quick" promises.