Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

You can deny a named User-Agent in an Apache .htaccess file with mod_rewrite. Put the rule before broad application rewrites:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBot|AnotherBot) [NC]
RewriteRule ^ - [F,L]

This returns HTTP 403 for matching requests while leaving the requested URL unchanged. Replace the sample names with a distinctive fragment from your access logs. User-Agent filtering is useful for low-effort unwanted clients, but it is not security against a determined bot because the header is controlled by the client.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the rule does

The three directives work together:

Directive or flag Purpose
RewriteEngine On Enables per-directory rewrite processing in this .htaccess file.
RewriteCond %{HTTP_USER_AGENT} (BadBot|AnotherBot) [NC] Checks the incoming User-Agent request header. The parentheses contain a regular expression; the vertical bar means “or.” [NC] makes the comparison case-insensitive.
RewriteRule ^ - [F,L] Matches the request path, substitutes nothing (the hyphen), returns a forbidden response with [F], and stops the current rewrite round with [L].

With a document-root .htaccess, the ^ pattern matches every path handled by that directory. The request is denied before your application receives it, provided no earlier rule has already handled the request.

Check prerequisites before editing

.htaccess rules only work when the server is configured to read them. Confirm these points with your administrator or hosting documentation:

#1 Best Overall
Sale
Compressed Air Duster,130000RPM Super Power Cordless Air Duster,Rechargeable Brushless Blower,3 Gear Adjustable Blower with LED Light,Dust Remover for Computer/House/Outdoor/Car
  • Powerful Turbo Fan:CAGIWIRU Electric Air Duster have reached speed up to 130000RPM,Can efficient remove dust and debris.With LED light and Three Adjustable Speed to meet with different cleaning tasks.
  • With Attached USB C Fasting Charging cable.Charing 3 hours,Enjoy up to 240 minutes of use on the lowest setting, For third speed can use for 25mins.with four charging options to meet with your needs.
  • Wide Application:Upgraded attached 5 different nozzles,making it suitable for a variety of scenes, such as car,pet, pc, keyboards, and other electronic devices. It also serves for office and home clean duster.
  • Handy Design & Portable:Cordless air duster electric nozzle is Light weight and small size, design and comfortable to hold and space saving, convenient to carry around. You can put it in your room or inside the car without taking up space.
  • Warranty & Support: Our Electric air duster comes with 5 years warranty and 24/7 customer service. During this period, if you have any questions,pls kindly feel free to contact us.
  • The site is served by Apache HTTP Server, or by a compatible configuration that supports Apache per-directory directives.
  • Overrides are permitted for the directory containing the file. An Apache AllowOverride policy can disable all or part of .htaccess processing.
  • mod_rewrite is loaded if you plan to use RewriteCond and RewriteRule.
  • You can identify the unwanted client’s User-Agent from reliable access-log entries.
  • You have a way to restore the previous file if a syntax error causes a server error.

The title alone cannot establish any of these server-specific settings. A managed host may require support to enable overrides or the rewrite module.

Install a site-wide User-Agent block

  1. Back up the existing file. Download or copy the current .htaccess before making changes. Preserve comments and any front-controller rules used by your CMS or framework.
  2. Choose a distinctive match. Inspect the access log and copy a stable part of the header. If the unwanted client consistently starts its value with a name, anchor the expression, for example ^NameOfBadRobot. If several names are involved, separate them with |.
  3. Open the correct file. For a whole site, use the .htaccess in the document root. A file in another directory has a narrower scope.
  4. Place the block before catch-all rewrites. Insert it before rules that route every request to an index file or otherwise perform broad processing. This gives the denial a chance to run before a later rule changes the request’s path.
  5. Save and check configuration. If you have server access, run the configuration test appropriate to your Apache installation before reloading. Without that access, make one small edit at a time so you can quickly restore the backup if Apache reports an error.
  6. Reload with two identities. Send a request using the target User-Agent and another using a normal browser value. The first should receive 403; the second should follow the site’s ordinary response path.

A complete root-level example looks like this:

# Keep this before the application's catch-all rules
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot [NC]
RewriteRule ^ - [F,L]

Limit the block to a directory

You do not have to deny the client everywhere. In a document-root file, add the directory name to the rule pattern:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBot|AnotherBot) [NC]
RewriteRule ^private/ - [F,L]

Only requests whose path begins with private/ are blocked by this rule. Adjust the path to your actual sensitive subtree.

Per-directory matching is a common source of mistakes. Apache removes the directory prefix before evaluating a RewriteRule in that directory’s own .htaccess. For example, if the file is inside /private/, use:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (BadBot|AnotherBot) [NC]
RewriteRule ^ - [F,L]

In that file, ^ covers the requests handled inside /private/; writing ^private/ there would look for an additional nested directory and normally match nothing. Put a rule in the narrowest directory that provides the intended scope.

Rank #2
EVEO® Screen Cleaner Spray Kit (Streak-Free) TV Screen Cleaner for Smart TV, Lcd, LEd, Oled | Computer Screen Cleaner Spray and Wipe Microfiber Cloth - TV, Monitor, Laptop, iPad, Car - 6.7oz 1 Pack
  • Screen Cleaner is Your Screen’s New Bestfriend! - EVEO is proud to present a screen cleaner that’s perfectly suited for your TV. Meet your new screen cleaner, computer screen cleaner, laptop cleaner, iPad cleaner, Macbook,tv cleaner or any other electronic device. Our screen cleaner spray and wipe along with the included plush microfiber cloth, easily removes dust, fingerprints & other smudges on the screen’s surface. Clean effectively for a streak-free screen
  • Gentle on your Screens - Our screen cleaner spray and wipes is compatible to be used even for the most sensitive LCD, LED, CRT, and OLED screens in proper use. Be at ease knowing that this product is compatible with all the major brands: LG, SONY, Samsung, Sharp, iPad, iPhone, Android screens, Kindle, PC monitors - we’ve got you covered! Can be used as LCD screen cleaner, tv cleaner, smart tv screen cleaner, and more
  • Premium Super Soft Plush Microfiber Cloth - Included in this efficient screen cleaning kit. This screen cleaner spray and wipe has our signature plush microfiber cloth specially designed to comply with screens and monitors and leave them streak-free. Has been shown to effectively absorbs stubborn stains, zero streaks, and no fiber shedding. Always use this together with our screen cleaner for the best results
  • Easy-to-Use Screen Cleaner Kit + microfiber cleaning cloth - The EVEO Screen Cleaning Kit will easily be your new favorite tool when it comes to taking care of the things you love most, quick and easy solution for a streak-free screen. Simply screw on the sprayer gun on the screen cleaner bottle. Spray the cleaning liquid onto the microfiber cloth, then wipe the screen until desired cleanliness & shine is achieved.
  • Quality Screen Care Kit Worth Purchasing - Enjoy a convenient Screen Cleaner anytime, anywhere you need it. Your satisfaction is our top priority. We stand behind the quality of our products and that’s why, you shouldn’t be worrying at all. We manufacture the Best Screen Cleaner. and can be used for laptop cleaning kit, laptop screen cleaner, tv screen cleaner for smart tv. MacBook screen cleaner, monitor cleaner or screen cleaner spray for electronic devices with microfiber cleaning cloth

Build safer User-Agent expressions

Match a known prefix

When the log shows values beginning with one exact product name, use an anchor:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot [NC]

The caret requires the value to start with that text. This reduces accidental matches where the same letters appear in an unrelated browser or crawler description.

Match several named clients

Use a grouped alternative when the same response should apply to multiple signatures:

RewriteCond %{HTTP_USER_AGENT} (BadBot|AnotherBot|ExampleCrawler) [NC]

Keep each alternative as specific as possible. A short fragment such as bot can match legitimate tools, monitoring agents, and browsers that happen to include those letters.

Use a bounded expression when necessary

If a name can appear inside a longer token, add boundaries or surrounding context that reflects the real log value. Test the expression against known good and bad headers before deploying it. Regular-expression syntax is case-insensitive here because of [NC], but punctuation and spacing still matter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
BladeHawks RGB Gaming Mouse Pad Extra Large Extended 31.5x12 Inch-Black
  • 【Large size】:Measuring 800 x 300 mm/ 31. 5×12 inches, which is wider and taller than others. Large mouse pad can perfectly fit your keyboard and mouse of any size, and there is also ample space for peripherals such as phones, tablets, etc.
  • 【LED backlight mouse pad】:Blade Hawks RGB mouse pad provides 7 static modes and 7 dynamic modes will give you the options you are looking for. Power-off memory function remembers the last lighting mode you selected. The super glow fiber Chroma will give you a colorful gaming setup that you want.
  • 【Anti-slip rubber base】:Made from Natural rubber, this gaming mousepad will firmly grip your desktop, allowing you to enjoy the ultimate precision in the games you are most passionate about. It also comes with a texture that enhances this ability even further.
  • 【Smooth and waterproof surface】: Micro-textured cloth surface, for extremely precise mouse control and a smooth movement. When liquid splashes on the gaming mouse pad, it forms water droplets and slides down. Will not delay your work or gameplay.
  • 【Plug and play】: This mouse and keyboard pad is powered by USB, plug and play, no driver required. One button control light modes. Press one time to change the lighting mode, press twice to change the brightness, press and hold about 3 seconds turn ON/OFF.

Do not treat a missing header as proof of a bot

The condition above matches only the text you specify. A client that omits the header, changes it, or sends a new value will not be caught unless you add a separate condition. Broadly denying empty or unfamiliar headers can block legitimate clients, so make that decision only after reviewing your traffic.

Use SetEnvIfNoCase when you do not need mod_rewrite

Apache also supports an authorization-style approach. It marks matching requests with an environment variable and then denies requests carrying that marker:

SetEnvIfNoCase User-Agent ^NameOfBadRobot goaway

<RequireAll>
    Require all granted
    Require not env goaway
</RequireAll>

Use this in a location or directory context where both directives are allowed. SetEnvIfNoCase performs a case-insensitive match on the User-Agent header. The RequireAll block grants normal access, then excludes requests for which the goaway marker was set.

This method is useful when your existing configuration already uses Apache 2.4 authorization directives and you want to avoid adding rewrite rules. It still depends on the same .htaccess override policy and has the same fundamental weakness: the header is supplied by the client.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose termination and response behavior

[F,L] for a straightforward denial

For a simple block, [F,L] is the usual choice. [F] causes Apache to return a forbidden response (HTTP 403), while [L] stops processing the remaining rules in the current rewrite round. The URL is not redirected and the request is not handed to the application.

Rank #4
Sale
Vaydeer Wrist Rest for Keyboard and Mouse, Computer Ergonomic Wrist Support Pad, Soft Memory Foam Arm Cushion for Desk, Palm Hand Office Laptop Typing
  • 【Softer and More Comfortable】Vaydeer wrist rest has unique diamond pattern, which is the combination of softness and aesthetics. The materials of wrist rest are improved into higher quality memory foam and covered with silky smooth lycra. The computer wrist rest makes you as comfortable and cushiony as like rest your wrists on clouds.
  • 【Ergonomic Wrist Saver】The wrist rests for keyboard and mouse comes with a 17.32×3.15×0.83 inch keyboard wrist pad and a 5.94×3.15×0.83 inch mouse wrist support. Based on ergonomic design, the unique concave shape is the perfect fit for your wrist joints. The wrist rest pad fits most computer keyboards and laptops, improve hand and wrist posture, release your wrist and arm stress.
  • 【Non-Slip Rubber Bottom】Featuring an anti-skid silicone base on the bottom, this wrist keyboard support stays firmly in place on your desk, preventing the padding from sliding around, ensuring stable and consistent wrist support during extended computer sessions.
  • 【Better Experience & Pain Relief】Our keyboard arm rest is beneficial to alleviate the soreness caused by direct contact and friction between your arm and a hard desk surface, reducing the risk of wrist fatigue or carpal tunnel. The soft texture of memory foam can evenly distribute the pressure around your wrists and provide good support with just enough give.
  • 【Helpful in Multiple Scenarios】Whether you're working, studying, writing, typing, gaming, this keyboard and mouse rest combo is an essential accessory to add comfort and support to your hands and wrists. It’s also a great gift for men, women, family, friend, coworker, gamer, teacher, etc.

[END] on Apache 2.4 when re-entry matters

Apache 2.4 provides [END], which stops all per-directory rewrite processing and prevents another rewrite pass for the current request. Consider it when nested .htaccess files or repeated per-directory rounds could otherwise re-enter the rewrite engine. Check the site’s existing rewrite flow before replacing [L]; a blanket change can alter unrelated rules.

Do not hide a denial behind a redirect

A redirect sends the client to another URL and can make the unwanted request reach your application or another endpoint. If the intent is to refuse service, a 403 response communicates that decision directly. Application-level handling may be appropriate for a different goal, such as presenting a challenge, but that is not equivalent to an Apache block.

Rule ordering with CMS and front-controller rules

Many sites contain a catch-all rule that rewrites requests to an index script. If that rule appears first, the request may be transformed before your User-Agent condition runs, or it may trigger additional per-directory passes. Put the denial near the top of the relevant rewrite section, after any directives that must initialize the environment but before broad routing rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not delete or reorder unrelated rules casually. A misplaced rewrite can break static files, canonical redirects, HTTPS enforcement, or administrative paths. If the site’s existing configuration has several rewrite sections, preserve their structure and add the smallest possible block in the section that processes the target requests.

Test the block safely

Testing should compare the blocked identity with a normal one and verify both the status code and server behavior. For example, with a command-line HTTP client:

Best Value
Sale
Under Desk Cable Management Tray No Drill, Metal Mesh Cable Management Under Desk with Clamp Mount, 2 Hole Cable Tray with Wire Management and Cord Organizer for Home Office Standing Desk
  • 【No Drill Mounted】Cable management tray can be assembled in as little as 3 minutes or less-simply. Side clips easily clampe the edge of desk, pass through the holes on the sides, and secure excess cables with baskets. Desk cable management supports inward or outward installation to meet your needs in different scenarios, and provides you with great convenience when collecting and organizing wires.
  • 【Rubber Pad Protects Desk】Wire organizer under desk built-in soft rubber pads on the clamp protect your desk from scratches, so you will not damage your furniture or office.
  • 【Sturdy and Beautiful】Cord management under desk is made of high-quality carbon steel and holds about 15 lbs. The wire basket has 2 holes on each side of the tray to enable your cables to pass freely. Cable baskets are suitable for desk thickness use from 0.4" to 2.4".
  • 【Space Saving & Keep Organized】Desk cable management tray holds all your power cords, outlet strips, USB hubs and computer transformers hidden from your feet, and the data cables will not enter the vacuum cleaner to keep your desk clean and tidy. Desk wire organizer is also suitable for use in the kitchen and outdoors.
  • 【Reduce Safety Risks】Cable tray under desk keeps all plugs away from your kids, no more worry about tripping. Using 2 holes wire tray saves extra space, helps you easily cabling and wire protection. The mesh design is not easy to accumulate dust, but avoids safety accidents caused by messy wires.
curl -i -A "NameOfBadRobot 1.0" https://example.test/path
curl -i -A "Mozilla/5.0" https://example.test/path

Replace the hostname and path with your own site. The first request should show a 403 status if the expression matches. The second should produce the status normally expected for that URL. Test a path inside and outside the scope when you used a directory prefix.

Review the Apache error log after each change. A syntax error, a disabled directive, or an unexpected rewrite loop is usually recorded there even when the browser displays only a generic 500 error. Test over HTTPS if that is how real traffic reaches the site, and repeat the check against a URL that normally reaches your application so you know the block runs before front-controller routing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

The server returns 500 after editing

  • Restore the backup to confirm the edit caused the failure.
  • Check the error log for an invalid regular expression, an unsupported directive, or a directive disallowed in .htaccess.
  • Verify that mod_rewrite is loaded when using RewriteEngine, RewriteCond, or RewriteRule.
  • Reapply the change in a minimal form, then add alternatives one at a time.

The named client is not blocked

  • Compare the exact logged header with the expression, including punctuation, spaces, and version text.
  • Remove an overly strict anchor only if the log shows the name is not at the beginning.
  • Confirm the request reaches the directory containing the file and that the host permits overrides there.
  • Check rule order: a prior rule may have handled the request before this condition was evaluated.
  • Make sure you tested the same hostname, protocol, and path that the client uses.

Legitimate visitors are blocked

  • Search the expression for a fragment shared with common browser, preview, monitoring, or accessibility tools.
  • Replace a short substring with a distinctive prefix or a more tightly bounded expression.
  • Remove the offending alternative and retest with representative normal User-Agent values.

The root rule works but a subdirectory rule does not

Remember that Apache strips the directory prefix before matching a rule in that directory’s own file. Use ^ for everything in that file, not the directory name again. Conversely, a root-level file needs the prefix when you intend to restrict the rule to a subtree.

The request still reaches the application

Move the block above catch-all rewrites and inspect nested .htaccess files. If repeated per-directory processing is involved on Apache 2.4, evaluate whether [END] is appropriate for this site’s rewrite design.

Understand the security limitation

Apache’s documentation warns: “Any technique that relies on the USER_AGENT string can be trivially circumvented, since that string can be changed by the client.” A blocked crawler can simply send a different value, copy a browser’s value, or omit the header. Consequently, a User-Agent rule is a traffic filter, not authentication, access control for sensitive data, or proof that a request is automated.

For sustained abuse, use a control that observes signals the client cannot change as easily. Depending on the threat and your architecture, that can include firewall rules, IP reputation or allow/deny policies, rate limiting, authentication, application-level bot controls, or a CDN/WAF. These controls address different failure modes; none should be assumed to replace the others without measuring the traffic you need to stop.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compare the two Apache approaches

Axis mod_rewrite SetEnvIfNoCase plus Require
Required capability mod_rewrite and permission for rewrite directives in the relevant .htaccess context. Permission for SetEnvIfNoCase and Apache 2.4 authorization directives in that context.
Typical scope Whole site from the document root, or a subtree by changing the rule pattern and file location. The directory or protected location where the environment and authorization directives are applied.
Normal response 403 with [F]; the URL remains unchanged. Authorization failure for requests marked with the environment variable.
Termination control [L] ends the current rewrite round; Apache 2.4 [END] can stop per-directory re-entry. Handled by authorization processing rather than rewrite termination flags.
Maintenance Convenient when the site already has rewrite rules, but ordering must be managed carefully. Clear separation between matching and authorization, provided both directive families are permitted.
Resistance to spoofing Weak: the User-Agent is client-controlled. Weak: the same client-controlled value is used.

The security column is decisive for threat modeling: changing implementation style does not make a User-Agent check trustworthy against a determined client.

Maintain the block over time

  • Review access logs periodically to confirm the signature still appears and to discover variants that use a different header.
  • Keep alternatives specific; remove entries that create false positives or no longer represent unwanted traffic.
  • Document why each name was added, which paths it covers, and who should be contacted before changing it.
  • Retest after Apache, CMS, or hosting changes because override policies and rewrite ordering can change.
  • Watch response and error logs for unexpected 403 increases after every edit.

If the unwanted traffic is costly or persistent, treat the .htaccess rule as a temporary or low-effort filter while implementing a control that does not depend solely on a self-declared header.

Final deployment checklist

  1. Confirm Apache, override permissions, and the availability of the module or directives you selected.
  2. Back up the existing .htaccess and preserve application routing rules.
  3. Copy a distinctive User-Agent fragment from logs and anchor it when an exact prefix is intended.
  4. Choose root-wide or directory-only scope and place the file and pattern accordingly.
  5. Insert the denial before broad catch-all rewrites.
  6. Use [F,L] for a normal 403, or assess [END] on Apache 2.4 when per-directory re-entry is a concern.
  7. Test blocked and normal User-Agents, in-scope and out-of-scope paths, and inspect the logs.
  8. Escalate to rate limits, network controls, authentication, application defenses, or a CDN/WAF when clients can spoof the header or the attack continues.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.