To block selected bot requests on Apache, you can deny access using a rule in the site's .htaccess file, provided your host permits that configuration. Start with a specific user-agent or confirmed abusive IP address, then verify that ordinary visitors can still use the site.
We explain how to block bots with current Apache 2.4 syntax. These rules are targeted filters, not complete bot detection: a client can change its user-agent header, and robots.txt only works when a crawler chooses to obey it.
Decide what you actually want to stop
Not every automated request is harmful. Search crawlers and other integrations may be useful to your website. We recommend describing the unwanted behavior before writing a rule: repeated requests to a sensitive route, an unwanted crawler identifying itself consistently, or a source making requests you have decided not to accept.
Review your host's access logs and application records. Make a note of the requested paths, source addresses, user-agent strings, and response codes. Treat a traffic spike or a short visit as a reason to investigate, not proof of malicious activity. Build the rule around evidence you can inspect.
Do not turn a list of software names into a universal blacklist. A header mentioning curl, a programming library, or a browser does not establish the caller's intent. Equally, a header claiming to be a search engine is not proof of identity: Apache explicitly warns that user-agent access control is unreliable.
Need the domain first?
Runs the same live check as the front page. If it is taken we will show you when it expires.
Step 1: check the server and prepare a rollback
This tutorial is for Apache 2.4, with the required directives allowed in the relevant directory. If you are unsure what serves your site, review how to identify a website's hosting and ask the hosting provider which configuration applies. Do not assume a file called .htaccess controls every hosting environment.
Apache uses AllowOverride and AllowOverrideList to determine which directives a directory-level file may contain. With overrides disabled, the file can be ignored. A forbidden directive or syntax mistake can also cause an error. You cannot fix a server-level permission restriction by adding more directives to the same file.
- Use your hosting provider's supported file-access method to locate the site's document root.
- Find the existing
.htaccessfile and save a separate backup outside the public web directory. - Keep existing application, redirect, and access-control configuration intact.
- If creating a new file, confirm that directory-level overrides are supported first.
Read our introduction to .htaccess if the file's scope is unfamiliar. Apache recommends the main server configuration instead when you have access to it; the directory-file approach is useful when that access is unavailable.

Step 2: block an exact user-agent value
For a simple Apache 2.4 example, the following rule denies a request whose user-agent is exactly ExampleUnwantedBot. It is a placeholder, not the name of a real bot we have identified on your site.
<If "%{HTTP_USER_AGENT} == 'ExampleUnwantedBot'">
Require all denied
</If>Replace the placeholder with the exact value you intend to reject. Exact matching means a longer header containing that word will not match this example. We prefer showing that limitation explicitly rather than giving a broad expression that could reject unintended clients.
Add the block in the scope you intend to protect. A document-root file affects that directory and its descendants, subject to Apache's configuration-merging rules. On sites that already have authorization rules, have the combined configuration reviewed rather than assuming a new block is independent of existing restrictions.
Older tutorials often use Order, Allow, and Deny, sometimes with an environment variable. Apache documents those compatibility directives as deprecated. Do not mix an old Deny from env=... snippet into a new authorization policy without understanding how the configurations interact.
Step 3: test the block and normal access
Save or upload the edited file. From a terminal with curl installed, use your real site URL in place of the example address. These commands make ordinary GET requests and print the returned headers while discarding the body.
curl -sS -D - -o /dev/null -A 'ExampleUnwantedBot' https://example.com/
curl -sS -D - -o /dev/null -A 'ExampleVisitor' https://example.com/The matching request should be denied; compare it with the nonmatching request and the site's normal behavior. A redirect or cached response can complicate the comparison, so test a final page URL you know is served from the protected scope. Do not claim success solely because the file saved.
Also browse important pages and test any forms, monitoring, or integrations you rely on. If normal traffic fails or the server returns an unexpected error, restore the backup and consult the Apache error log. Make one small change at a time so the cause remains identifiable.
Blocking a confirmed IP address instead
Apache also supports IP-based authorization. This alternative denies a single illustrative address while allowing other requests through this particular authorization block:
<RequireAll>
Require all granted
Require not ip 192.0.2.123
</RequireAll>Replace the address with one you have verified should be blocked. Do not paste the example unchanged expecting it to stop your unwanted traffic. The positive requirement matters: Apache explains that a negated requirement cannot authorize a request by itself.
We recommend reviewing existing restrictions and any proxy setup with your host before deploying an IP rule. Keep the block narrow, record why it exists, and retest useful traffic. It is not a method for identifying every request made by the same bot operator.
When robots.txt or Cloudflare is more appropriate
Use robots.txt when the aim is to communicate crawling preferences to cooperative crawlers. Google states that those instructions cannot force compliance and are not a way to secure private files. A URL blocked from crawling can also still appear in search results if discovered elsewhere.
For broader mitigation, Cloudflare's Bot Fight Mode documentation describes pattern-based bot detection and challenges. It also warns that API or mobile-app traffic may be challenged and that WAF custom rules cannot bypass Bot Fight Mode. Check those limitations before enabling it.
If you are sorting out who controls the website before editing it, our WHOIS lookup can help investigate domain registration details. Check hosting-file access separately: registration information does not establish which Apache directives your account may use. Our Bot Fight Mode guide covers the separate Cloudflare approach.
Frequently asked questions
Will a user-agent rule stop a determined bot?
No. A client can send a different header. Treat the rule as a way to reject a particular declared identity, not as authentication or comprehensive abuse prevention.
Should we block all search crawlers?
Only if that matches your actual publishing goal. We recommend deciding which automated access is useful before deploying a blanket rule. Blocking requests and controlling search indexing are different tasks.
What if nothing changes after editing .htaccess?
Check the file location, the actual server configuration, and whether the host allows the directives. If an error appears instead, restore the backup and use the error log to identify the unsupported directive or syntax problem.