Guide SSL & Security

How to block an IP address from your site

Two reliable ways to stop a specific address reaching your site, and why blocking one IP rarely stops a determined, repeated attacker for long.

Updated 6 min read Intermediate

Blocking an IP address stops requests from that specific address reaching your site at all. It is a blunt tool — effective against a single persistent source, and much less effective against anything using many different addresses — but for the right situation it is quick to apply and needs no ongoing maintenance once it is in place.

When it is the right tool

Blocking a specific address is worth doing when you can identify one clearly responsible for a problem: repeated failed login attempts from the same source, a single address generating an unusual volume of requests, or a specific address you have independently confirmed is behind abusive behaviour. It is a poor tool against distributed attacks using many addresses at once, since blocking them one at a time cannot keep pace — that situation calls for rate limiting or a service designed to filter traffic before it reaches your site, covered in what a web application firewall does.

Finding the address to block

Check your site's access logs, available through the control panel, for the specific address responsible. Confirm you have the exact address rather than a partial match or a description, since blocking the wrong address either misses the actual source or blocks something unrelated.

Do not block your own address by mistake

Before applying any block, double-check the address is not your own current connection or one shared with legitimate visitors — a large office or organisation, for instance, may show many people sharing one visible address. Blocking a shared address can lock out far more people than intended.

Method 1: your control panel

Most control panels include a dedicated IP deny or IP blocking tool for exactly this, without needing to edit any files directly. Open the relevant security section for the domain, add the address you want to block, and save. This is the safer option for most cases, since it avoids editing .htaccess by hand and the risk of a small syntax mistake affecting the whole site.

Method 2: adding a rule to .htaccess

For more direct control, or where the control panel tool does not cover your specific need, a rule can be added to the .htaccess file in your site's root folder. Using the modern Apache 2.4 syntax:

<RequireAll>
    Require all granted
    Require not ip 203.0.113.55
</RequireAll>

This grants access generally but explicitly excludes the listed address. To block more than one, add additional Require not ip lines within the same block, one address per line.

Blocking a range instead of a single address

If an attacker is rotating through several nearby addresses, you can block a whole range using CIDR notation, for example Require not ip 203.0.113.0/24, which covers every address from 203.0.113.0 to 203.0.113.255. Use this deliberately and sparingly, since a wider range risks blocking unrelated, legitimate visitors who happen to share part of the same address range.

Back up before editing .htaccess

As with any change to this file, keep a copy of the original before editing, and check the site loads correctly immediately after saving. A small syntax error in this file is one of the few mistakes capable of taking an entire site offline instantly, so confirming the change worked as intended straight away matters more here than with most other settings.

Confirming the block actually worked

After adding a block, check your access logs for further requests from that address. A properly applied block should show the requests stopping, or being met with a forbidden response, rather than continuing to reach your application as before. If the activity continues unchanged, double-check the exact address was entered correctly — a single mismatched digit means the rule matches nothing at all — and confirm you edited the correct .htaccess file if your site has more than one, such as a separate one inside a subfolder that could be taking precedence for requests to that path.

Removing a block later

Blocks accumulate over time if nothing is ever removed, and old entries add up to a list nobody remembers the reason for. Review the list periodically: remove entries for addresses that have not attempted anything in a long time, and keep a short note of why each remaining one was added, since "blocked six months ago for a reason nobody now recalls" is not a useful state for a security control to be in.

Why a single block is rarely the whole answer

An address is a comparatively easy thing for an attacker to change, particularly for anything automated. If the same kind of activity resumes shortly after a block from a different address, treat the block as a temporary measure and add the more durable protections it is meant to buy you time for: strong, unique passwords, two-factor authentication, and a limit on repeated failed login attempts, covered in how to stop brute-force login attacks.

Frequently asked questions

Will blocking one IP address stop an attack for good?

Usually not on its own. A determined attacker using automated tools can switch to a different address easily, so a single block is best treated as one part of a response rather than a complete fix — combine it with strong passwords, two-factor authentication and a login attempt limit for something more durable.

Related reading