{"id":20431,"date":"2025-07-10T11:09:06","date_gmt":"2025-07-10T15:09:06","guid":{"rendered":"https:\/\/kb.n0c.com\/?post_type=ht_kb&#038;p=20431"},"modified":"2026-08-31T15:11:54","modified_gmt":"2026-08-31T19:11:54","slug":"how-to-use-htaccess-to-filter-out-malicious-robots-and-requests","status":"publish","type":"ht_kb","link":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/","title":{"rendered":"How to Use .htaccess to Filter Out Malicious Robots and Requests"},"content":{"rendered":"<div class=\"wp-block-post-author-name\">Fabrice Q. Conseiller Technique<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In this article, we will look at how to block malicious robots and requests with the .htaccess file.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\"><strong>Note<\/strong>: The <a href=\"https:\/\/kb.n0c.com\/en\/glossary\/\" target=\"_blank\" rel=\"noreferrer noopener\">glossary<\/a> contains explanations on a wide range of subjects and can be consulted to clarify certain terms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>Website traffic does not just come from human visitors. A large proportion of requests are generated by robots: some are legitimate (like search engines), but others are malicious. The later can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>unnecessarily consume server resources;<\/li>\n\n\n\n<li>attempt to exploit security vulnerabilities; or even<\/li>\n\n\n\n<li>participate in DDoS attacks.<\/li>\n<\/ul>\n\n\n\n<p>Some requests may also come from suspicious IP addresses or user agents known to scan, hack or disrupt Web services. In some cases, it is useful to set up simple rules to block these unwanted accesses before they even reach your scripts or application.<\/p>\n\n\n\n<p>The .htaccess file, used with a server such as Apache, enables you to filter this type of traffic upstream, with rules that are easy to adapt to your needs. This guide will show you how to implement these rules safely and effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequis\">Prerequisites<\/h2>\n\n\n\n<p>Enter the following address in your <a href=\"https:\/\/mg.n0c.com\" target=\"_blank\" rel=\"noreferrer noopener\">web browser<\/a>: <a href=\"https:\/\/mg.n0c.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/mg.n0c.com<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"marche-a-suivre\">How to Filter Upstream Traffic<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 &#8212; Identify Malicious Requests<\/h3>\n\n\n\n<p>Locate malicious requests in the access log (see <a href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/access-logs\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use Access Logs<\/a>).<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start by analyzing the site&#8217;s access log.<\/li>\n\n\n\n<li>In the example below, we see an IP address located in France sending requests to wp-cron.php, with an HTTP response code of 200 (success).\n<ul class=\"wp-block-list\">\n<li>As the application firewall has not blocked this request, it may be appropriate to intercept it directly via .htaccess, as it is not legitimate in the context of this website.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"369\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png\" alt=\"Access journal showing malicious request\" class=\"wp-image-20370\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png 1024w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-300x108.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-768x277.png 768w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-50x18.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-60x22.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-100x36.png 100w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse.png 1056w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>In the example below, we see a request from a user agent identified as BigBadRobot, accessing the file wp-cron.php with HTTP response code 200 (success).\n<ul class=\"wp-block-list\">\n<li>Since this request has not been blocked by the application firewall and this agent is known to behave in an abusive or illegitimate manner, it may be appropriate to block it directly via .htaccess.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-1024x304.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"304\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-1024x304.png\" alt=\"Blocking robot with .htacess\" class=\"wp-image-20461\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-1024x304.png 1024w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-300x89.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-768x228.png 768w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-50x15.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-60x18.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot-100x30.png 100w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-BigBadRobot.png 1282w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 2 &#8212; Locate the .htaccess File<\/h3>\n\n\n\n<p>Identify the document root for the domain or sub-domain on which you wish to block requests in the domain manager (please refer to the <a href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/domain-management\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Manage Domains<\/a> article).<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the Domain Manager, locate the document root corresponding to the domain (or sub-domain) on which you wish to apply the blocks.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document.png\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"107\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document.png\" alt=\"locate the document root corresponding to the domain\" class=\"wp-image-20447\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document.png 570w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document-300x56.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document-50x9.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document-60x11.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-gestionnaire-domaine-racine-document-100x19.png 100w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><\/figure><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Open the file manager and locate the .htaccess file (see <a href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/n0c-filemanager\/#:~:text=clarify%20certain%20terms.-,Prerequisite,-Access%20to%20https\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use the File Manager<\/a>).<\/li>\n\n\n\n<li>Access the file manager, then navigate to the document root identified in the previous step. Locate the .htaccess file.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-1024x244.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"244\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-1024x244.png\" alt=\"ht.access in File Manager\" class=\"wp-image-20445\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-1024x244.png 1024w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-300x72.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-768x183.png 768w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-50x12.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-60x14.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess-100x24.png 100w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-htaccess.png 1153w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>As .htaccess is a hidden file, it may be necessary to activate the display of hidden files in the Profile Settings.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"198\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1.png\" alt=\"Activating display of hidden files\" class=\"wp-image-20451\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1.png 676w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1-300x88.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1-50x15.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1-60x18.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/en-fichiers-caches-1-100x29.png 100w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Step 3 &#8212; Modify the .htaccess File<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the .htaccess file in edit mode. Here is an example of the contents of a default .htaccess file for a WordPress site:<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"348\" src=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress.png\" alt=\"Modify the .htaccess File\" class=\"wp-image-20393\" srcset=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress.png 557w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress-300x187.png 300w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress-50x31.png 50w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress-60x37.png 60w, https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/htaccess-wordpress-100x62.png 100w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><\/figure><\/div>\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Add the necessary blocking rules.<\/li>\n\n\n\n<li>Then add the appropriate blocking directives, as we will see in the examples below.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 &#8212; Clear the LSCache Cache<\/h3>\n\n\n\n<p>For the rules to take effect, it may be necessary in N0C to clear the cache for the domain in question (see the article <a href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-lscache\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use LSCache<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Important: Avoid Conflicts Between Blocking Methods<\/h2>\n\n\n\n<p>When setting up blocking rules in the .htaccess file, it is important <strong>not to combine several different methods to block the same target (IP, User-Agent, etc.) in the same context<\/strong>. For example, mixing directives, mod_rewrite and SetEnvIfNoCase rules to block the same IP addresses or agents can result in :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>conflicts of priority between rules;<\/li>\n\n\n\n<li>unexpected behaviour; or even<\/li>\n\n\n\n<li>incomplete blocking or, conversely, blocking that is too broad.<\/li>\n<\/ul>\n\n\n\n<p><strong>Tips to avoid these problems:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Choose a blocking method that is appropriate for your environment and needs (often <code>mod_rewrite<\/code> or <code>SetEnvIfNoCase<\/code> are sufficient).<\/li>\n\n\n\n<li>Group your rules together using the same logic, rather than duplicating blocks.<\/li>\n\n\n\n<li>Always test after each change.<\/li>\n\n\n\n<li>Document your rules clearly to facilitate maintenance.<\/li>\n<\/ul>\n\n\n\n<p>In summary, keep things simple and consistent to ensure effective filtering with no surprises.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to block IP addresses<\/h2>\n\n\n\n<p>There are several ways to block IP addresses in an .htaccess file. Here are some common approaches depending on the type of blocking you want.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Blocking Individual IP Addresses<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Method 1: via <code>mod_rewrite<\/code>(recommended)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\nRewriteCond %{REMOTE_ADDR} ^47\\.79\\.1\\.100$\nRewriteRule ^.* - &#91;F,L]<\/code><\/pre>\n\n\n\n<p>This method works at all levels of request processing, including less common HTTP methods. It is more reliable for strict blocking.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Method 2: Via the <code>&lt;Limit&gt;<\/code> Directives<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Limit GET POST&gt;\norder allow,deny\nallow from all\ndeny from 47.79.1.100\n&lt;\/Limit&gt;<\/code><\/pre>\n\n\n\n<p>This method is simple, but it only applies to <code>GET<\/code> and <code>POST<\/code> requests, and may be ignored by some clients. It is also less flexible than <code>mod_rewrite<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Blocking Multiple IP Addresses<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteCond %{REMOTE_ADDR} ^(47\\.79\\.1\\.100|47\\.82\\.1\\.100)$\nRewriteRule ^.* - &#91;F,L]<\/code><\/pre>\n\n\n\n<p>You can combine multiple IPs in the same regular expression, separated by <code>|<\/code> (logical OR).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Blocking IP Ranges (<a href=\"https:\/\/kb.n0c.com\/en\/glossaire\/ip-address\/\">CIDR<\/a> or Subnets)<\/h3>\n\n\n\n<p>Blocking entire ranges requires adapting the method used.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Method 1: With <code>mod_rewrite<\/code> (Precise and Flexible)<\/h4>\n\n\n\n<p>You can extend the pattern to cover an entire IP range based on the starting bytes (for example: <code>^47\\.79\\.<\/code> for a larger block).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\n# Bloque toute IP commen\u00e7ant par 47.79.1.\nRewriteCond %{REMOTE_ADDR} ^47\\.79\\.1\\.\nRewriteRule ^.* - &#91;F,L]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Method 2: With <code>&lt;Limit&gt;<\/code> (Simple, But Less Accurate)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Limit GET POST&gt;\norder allow,deny\nallow from all\ndeny from 47.79.0.0\/16\n&lt;\/Limit&gt;<\/code><\/pre>\n\n\n\n<p>CIDR notation (<code>\/16<\/code>) is not always supported in <code>.htaccess<\/code>. In this case, use prefix-based blocking instead.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Limit GET POST&gt;\norder allow,deny\nallow from all\ndeny from 47.82.0.\n&lt;\/Limit&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Blocking by <em>User-Agent<\/em> (Unwanted Robots)<\/h3>\n\n\n\n<p>Some abusive robots (or <em>crawlers<\/em>) identify themselves via their HTTP <em>User-Agent<\/em>. It is possible to block these agents using <code>.htaccess<\/code>. Here are two common methods.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Method 1 (Recommended Method): <code>SetEnvIfNoCase<\/code>(Simple and Effective)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SetEnvIfNoCase User-Agent \"GrandMechantRobot|BigBadRobot\" bad_bot\n\nOrder Allow,Deny\nAllow from all\nDeny from env=bad_bot<\/code><\/pre>\n\n\n\n<p>This method is simple, easy to read and generally compatible with modern servers. It allows you to easily block one or more unwanted <em>User-Agent<\/em> using an environment variable (<code>bad_bot<\/code>).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SetEnvIfNoCase<\/code>: allows you to ignore case in agent names.<\/li>\n\n\n\n<li>The field <code>\u2018GrandMechantRobot|BigBadRobot\u2019<\/code> contains the names of agents to be blocked, separated by <code>|<\/code> (logical OR).<\/li>\n\n\n\n<li><code>Deny from env=bad_bot<\/code>: blocks requests marked with this variable.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine on\nRewriteCond %{HTTP_USER_AGENT} GrandMechantRobot|BigBadRobot &#91;NC]\nRewriteRule ^.* - &#91;F,L]<\/code><\/pre>\n\n\n\n<p>This method also works very well and offers finer control in complex contexts, or when combining multiple rules in the <code>.htaccess<\/code> file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Blocking by IP and <em>User-Agent<\/em> (Unwanted Robots)<\/h3>\n\n\n\n<p>In some cases, you may want to block a request <strong>only if it comes from both a specific IP address and a <em>malicious User-Agent<\/em>.<\/strong><\/p>\n\n\n\n<p>Here is how to do it with <code>mod_rewrite<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine On\n\n# If the IP address matches...\nRewriteCond %{REMOTE_ADDR} ^47\\.79\\.1\\.100$\n\n# ...and that the User-Agent is one of the following...\nRewriteCond %{HTTP_USER_AGENT} (GrandMechantRobot|BigBadRobot) &#91;NC]\n\n# ...then we block the request (403 Forbidden)\nRewriteRule ^.* - &#91;F,L]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">How it Works<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The conditions are combined logically (AND)<\/strong>: all <code>RewriteCond<\/code> must be true for the rule to apply.<\/li>\n\n\n\n<li><code>[NC]<\/code> allows case to be ignored in agent names (for example, <code>grandmechantrobot<\/code> will also be blocked).<\/li>\n\n\n\n<li><code>[F,L]<\/code> means: return a <strong>403 Forbidden<\/strong> code (<code>F<\/code>) and do not continue processing other rules (<code>L<\/code> = Last).<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Tips and Recommendations<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This method is ideal for blocking <strong>a targeted bot<\/strong> that is attempting to bypass general protections.<\/li>\n\n\n\n<li>If you want to block the IP <strong>or<\/strong> the User-Agent (instead of \u2018and\u2019), you must use two separate rules.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Redirect certain IP addresses to a static page<\/strong>. Some administrators prefer to redirect certain unwanted robots or IP addresses to a customised static page (e.g. a humorous 403 page) instead of returning a simple error code.<\/li>\n\n\n\n<li><strong>Use a CDN<\/strong> (Content Delivery Network) to automatically filter and mitigate some of the malicious traffic before it reaches your server.<\/li>\n\n\n\n<li>Refine your<code>robots.txt<\/code> file to guide legitimate bots and restrict access to sensitive parts of the site.<\/li>\n\n\n\n<li>Use <code>mod_rewrite<\/code> if you need flexible rules and effective blocking.<\/li>\n\n\n\n<li><strong>Always test your rules after making changes<\/strong> to avoid accidentally blocking yourself or preventing legitimate visitors from accessing your site.<\/li>\n\n\n\n<li><strong>Do not overuse IP blocking<\/strong>. This type of filtering should be used in conjunction with an application firewall or other upstream security systems.<\/li>\n\n\n\n<li><strong>Only block User-Agents identified as harmful<\/strong>, so as not to penalise legitimate traffic or block robots that are important for your web visibility.<\/li>\n\n\n\n<li><strong>Avoid overly generic patterns<\/strong> that could block beneficial visitors or robots.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Test Your Rules Via a Terminal<\/h3>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\">Please refer to the article <a href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-the-mg-panel-terminal\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use the MG Panel Terminal<\/a> for further information.<\/p>\n\n\n\n<p>To validate your User-Agent blocks, use a tool such as <code>curl<\/code> on a <a href=\"https:\/\/kb.n0c.com\/en\/glossaire\/terminal\/\">terminal<\/a> with a custom agent, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -A \"GrandMechantRobot\" https:\/\/votre-site.com<\/code><\/pre>\n\n\n\n<p>This allows you to verify that your <code>.htaccess<\/code> rules are working as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Protecting your website from malicious bots, DDoS attacks and suspicious requests is essential for preserving server resources and ensuring security.<\/p>\n\n\n\n<p>The .htaccess file offers a simple and effective solution for filtering unwanted IP addresses and <em>User-Agent<\/em> upstream, before they reach your applications.<\/p>\n\n\n\n<p>By implementing these rules, you improve the reliability and security of your Apache site against malicious traffic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will look at how to block malicious robots and requests with the .htaccess file. Introduction Website traffic does not just come from human visitors. A large proportion of requests are generated by robots: some are legitimate (like search engines), but others are malicious. The later can:&#8230;<\/p>\n","protected":false},"author":7,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[165],"ht-kb-tag":[504],"class_list":["post-20431","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-files","ht_kb_tag-htacess-to-filter-out-malicious-robots-and-requests"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use .htaccess to Filter Out Malicious Robots and Requests<\/title>\n<meta name=\"description\" content=\"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use .htaccess to Filter Out Malicious Robots and Requests\" \/>\n<meta property=\"og:description\" content=\"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/\" \/>\n<meta property=\"og:site_name\" content=\"N0C KB\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-31T19:11:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1056\" \/>\n\t<meta property=\"og:image:height\" content=\"381\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/\",\"url\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/\",\"name\":\"How to Use .htaccess to Filter Out Malicious Robots and Requests\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kb.n0c.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/fr-journal-acces-requete-malicieuse-1024x369.png\",\"datePublished\":\"2025-07-10T15:09:06+00:00\",\"dateModified\":\"2026-08-31T19:11:54+00:00\",\"description\":\"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/#primaryimage\",\"url\":\"https:\\\/\\\/kb.n0c.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/fr-journal-acces-requete-malicieuse-1024x369.png\",\"contentUrl\":\"https:\\\/\\\/kb.n0c.com\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/fr-journal-acces-requete-malicieuse-1024x369.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/kb.n0c.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Articles\",\"item\":\"https:\\\/\\\/kb.n0c.com\\\/en\\\/knowledge-base\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use .htaccess to Filter Out Malicious Robots and Requests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kb.n0c.com\\\/#website\",\"url\":\"https:\\\/\\\/kb.n0c.com\\\/\",\"name\":\"N0C KB\",\"description\":\"Official documentation of the N0C hosting platform\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kb.n0c.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use .htaccess to Filter Out Malicious Robots and Requests","description":"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/","og_locale":"en_US","og_type":"article","og_title":"How to Use .htaccess to Filter Out Malicious Robots and Requests","og_description":"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.","og_url":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/","og_site_name":"N0C KB","article_modified_time":"2026-08-31T19:11:54+00:00","og_image":[{"width":1056,"height":381,"url":"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/","url":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/","name":"How to Use .htaccess to Filter Out Malicious Robots and Requests","isPartOf":{"@id":"https:\/\/kb.n0c.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/#primaryimage"},"image":{"@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/#primaryimage"},"thumbnailUrl":"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png","datePublished":"2025-07-10T15:09:06+00:00","dateModified":"2026-08-31T19:11:54+00:00","description":"The .htaccess file enables to filter malicious robots and requests traffic. This guide shows how to easily implement rules.","breadcrumb":{"@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/#primaryimage","url":"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png","contentUrl":"https:\/\/kb.n0c.com\/wp-content\/uploads\/2025\/07\/fr-journal-acces-requete-malicieuse-1024x369.png"},{"@type":"BreadcrumbList","@id":"https:\/\/kb.n0c.com\/en\/knowledge-base\/how-to-use-htaccess-to-filter-out-malicious-robots-and-requests\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/kb.n0c.com\/"},{"@type":"ListItem","position":2,"name":"Articles","item":"https:\/\/kb.n0c.com\/en\/knowledge-base\/"},{"@type":"ListItem","position":3,"name":"How to Use .htaccess to Filter Out Malicious Robots and Requests"}]},{"@type":"WebSite","@id":"https:\/\/kb.n0c.com\/#website","url":"https:\/\/kb.n0c.com\/","name":"N0C KB","description":"Official documentation of the N0C hosting platform","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kb.n0c.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb\/20431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/comments?post=20431"}],"version-history":[{"count":51,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb\/20431\/revisions"}],"predecessor-version":[{"id":24057,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb\/20431\/revisions\/24057"}],"wp:attachment":[{"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/media?parent=20431"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb-category?post=20431"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.n0c.com\/en\/wp-json\/wp\/v2\/ht-kb-tag?post=20431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}