PH Ranking - Online Knowledge Base - 2025-09-17

Using .htaccess to Create Firewall Rules and Block Malicious Traffic

You can use the .htaccess file on an Apache web server to create firewall-like rules that block malicious traffic by denying access based on IP addresses, user agents, request methods, or other criteria. This helps protect your website from unauthorized or harmful visitors.

Key methods to create firewall rules and block malicious traffic using .htaccess include:

  • Blocking by IP address: Deny access to specific IPs or IP ranges known to be malicious, while allowing all others. For example:

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

    This blocks the IP 123.45.67.89 but allows everyone else.

  • Allowing only specific IPs: Restrict access to only trusted IPs, denying all others:

    <RequireAll>
      Require all denied
      Require ip 123.45.67.89
      Require ip 98.76.54.32
    </RequireAll>
    

    This is useful for admin areas or sensitive parts of your site.

  • Blocking by User-Agent: Deny requests from suspicious or known bad bots by matching their User-Agent string:

    SetEnvIf User-Agent "BadBot" BLOCK_USER_AGENT
    
    <RequireAll>
      Require all granted
      Require not env BLOCK_USER_AGENT
    </RequireAll>
    

    This blocks any request with "BadBot" in the User-Agent header.

  • Blocking by HTTP request methods: Restrict or block certain HTTP methods that are not needed or potentially dangerous (e.g., TRACE, DELETE):

    <IfModule mod_rewrite.c>
      RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|POST|OPTIONS)$ [NC]
      RewriteRule .* - [F,L]
    </IfModule>
    

    This denies requests using methods other than GET, HEAD, POST, and OPTIONS.

  • Combining .htaccess with PHP for logging and alerting: Redirect blocked requests to a PHP script for logging or alerting purposes, enabling monitoring of malicious activity.

Best practices when using .htaccess for firewall rules:

  • Always back up your existing .htaccess file before making changes.

  • Keep rules as simple and specific as possible to avoid unintended blocking.

  • Regularly review and update your rules based on server logs and new threat intelligence.

  • Combine IP blocking with other security measures like web application firewalls (WAFs) or security plugins for layered protection.

  • Use the modern Apache 2.4+ syntax with <RequireAll>, Require ip, and Require not ip directives instead of deprecated Order, Allow, and Deny directives.

In summary, by editing your site's .htaccess file, you can effectively create firewall rules that block malicious IPs, user agents, and unwanted HTTP methods, thereby reducing harmful traffic and enhancing your website's security.

Internet images

PH Ranking offers the highest quality website traffic services in Philippines. We provide a variety of traffic services for our clients, including website traffic, desktop traffic, mobile traffic, Google traffic, search traffic, eCommerce traffic, YouTube traffic, and TikTok traffic. Our website boasts a 100% customer satisfaction rate, so you can confidently purchase large amounts of SEO traffic online. For just 720 PHP per month, you can immediately increase website traffic, improve SEO performance, and boost sales!

Having trouble choosing a traffic package? Contact us, and our staff will assist you.

Free consultation

Free consultation Customer support

Need help choosing a plan? Please fill out the form on the right and we will get back to you!

Fill the
form