Firewall Rules
Comprehensive guide to configuring WordPress firewall rules for maximum protection. Learn how to block malicious traffic, prevent common attacks, and secure your site with Wp Admin's firewall management.
What Are Firewall Rules?
Firewall rules are security directives that control incoming and outgoing network traffic to your WordPress website. They act as a barrier between your site and potential threats, filtering requests based on predefined criteria such as IP addresses, request types, file paths, and behavioral patterns.
A properly configured firewall is your first line of defense against the majority of WordPress security threats, including brute force attacks, SQL injection, cross-site scripting (XSS), and directory traversal attempts.
Types of Firewalls We Use
- Web Application Firewall (WAF) â Filters HTTP/HTTPS traffic at the application layer
- .htaccess Rules â Server-level directives for Apache-based hosting
- Nginx Config Rules â Nginx-specific security configurations
- IP-Based Rules â Allowlists and blocklists for specific IP addresses/ranges
- Rate Limiting â Throttles excessive requests to prevent abuse
Essential WordPress Firewall Rules
These are the core firewall rules we recommend for every WordPress site. Each rule addresses a specific vulnerability class.
Block wp-config.php Access
Prevents direct access to wp-config.php, which contains your database credentials and security keys. This file should never be accessible via HTTP.
# Block access to wp-config.php <Files wp-config.php> Order allow,deny Deny from all </Files> # Alternative using mod_rewrite RewriteEngine On RewriteRule ^wp-config\.php$ - [F,L]
# Block access to wp-config.php location ~ /wp-config\.php { deny all; access_log off; log_not_found off; }
Restrict wp-admin Access
Limits access to the WordPress admin area by IP allowlisting or adding an extra authentication layer. This prevents unauthorized login attempts from unknown sources.
# Restrict wp-admin by IP (replace with your IPs) <IfModule mod_authz_core.c> <Directory /var/www/html/wp-admin/> Require ip 192.168.1.0/24 Require ip 203.0.113.0/24 </Directory> </IfModule>
Disable Directory Browsing
Prevents visitors from viewing the contents of your directories. This stops attackers from discovering sensitive files, backup files, and plugin structures.
# Disable directory browsing Options -Indexes
# Disable autoindex (directory listing) autoindex off;
Block wp-login.php Attacks
Protects the WordPress login page from brute force attacks by rate-limiting requests and blocking known malicious patterns in the User-Agent string.
# Block bad bots from wp-login.php <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_USER_AGENT} (bot|crawl|spider|slurp|scan) [NC,OR] RewriteCond %{HTTP_USER_AGENT} (w3af|nikto|nmap|sqlmap) [NC] RewriteRule ^wp-login\.php$ - [F,L] </IfModule>
Block XML-RPC Abuse
Restricts access to xmlrpc.php, which is commonly abused for DDoS amplification attacks, brute force login attempts, and pingback spam.
# Block xmlrpc.php (disable if using Jetpack or mobile apps) <Files xmlrpc.php> Order allow,deny Deny from all </Files>
Block SQL Injection Attempts
Detects and blocks common SQL injection patterns in URL parameters, query strings, and POST data before they reach WordPress.
# Block SQL Injection attempts <IfModule mod_rewrite.c> RewriteCond %{QUERY_STRING} (union.*select|union.*all.*select|concat.*\(|char\() [NC,OR] RewriteCond %{QUERY_STRING} (select.*from.*where|insert.*into.*values|delete.*from) [NC,OR] RewriteCond %{QUERY_STRING} (DROP\s+TABLE|DROP\s+DATABASE|UPDATE.*SET) [NC,OR] RewriteCond %{QUERY_STRING} (1=1|OR\s+1=1|AND\s+1=1) [NC] RewriteRule .* - [F,L] </IfModule>
Block XSS (Cross-Site Scripting)
Blocks cross-site scripting attempts by filtering out script injection patterns from URL parameters and request data.
# Block XSS attempts in query strings <IfModule mod_rewrite.c> RewriteCond %{QUERY_STRING} (<\s*script|javascript:|on\w+=|vbscript:) [NC,OR] RewriteCond %{QUERY_STRING} (document\.(cookie|write)|window\.location) [NC] RewriteRule .* - [F,L] </IfModule>
Complete .htaccess Security Ruleset
Below is our complete recommended security ruleset for Apache servers. Wp Admin automatically deploys and maintains these rules on your site.
# âââââââââââââââââââââââââââââââââââââââââââ # Wp Admin â WordPress Firewall Rules # Last updated: December 2024 # âââââââââââââââââââââââââââââââââââââââââââ RewriteEngine On # ââ Block wp-config.php ââââââââââââââââââ <Files wp-config.php> Order allow,deny Deny from all </Files> # ââ Disable directory browsing âââââââââââ Options -Indexes # ââ Block access to sensitive files ââââââ <FilesMatch "\.(engine|inc|install|make|module|profile|po|sh|sql|theme|tpl(\.php)?|xtmpl)$|^\..+|/LICENSE|/README$"> Order allow,deny Deny from all </FilesMatch> # ââ Block xmlrpc.php âââââââââââââââââââââ <Files xmlrpc.php> Order allow,deny Deny from all </Files> # ââ Block SQL Injection ââââââââââââââââââ RewriteCond %{QUERY_STRING} (union.*select|concat.*\(|char\() [NC,OR] RewriteCond %{QUERY_STRING} (select.*from.*where|1=1|OR\s+1=1) [NC,OR] RewriteCond %{QUERY_STRING} (DROP\s+TABLE|UPDATE.*SET) [NC] RewriteRule .* - [F,L] # ââ Block XSS Attempts âââââââââââââââââââ RewriteCond %{QUERY_STRING} (<\s*script|javascript:|on\w+=) [NC] RewriteRule .* - [F,L] # ââ Block file inclusion attacks âââââââââ RewriteCond %{QUERY_STRING} (\.(php|pl|py|jsp|asp|txt|html|log|sh|cgi)) [NC,OR] RewriteCond %{QUERY_STRING} (file://|ftp://|php://) [NC] RewriteRule .* - [F,L] # ââ Block bad bots âââââââââââââââââââââââ RewriteCond %{HTTP_USER_AGENT} (w3af|nikto|nmap|sqlmap|nessus) [NC] RewriteRule .* - [F,L] # ââ Force HTTPS (if SSL enabled) âââââââââ RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # ââ Security Headers âââââââââââââââââââââ Header set X-Content-Type-Options "nosniff" Header set X-Frame-Options "SAMEORIGIN" Header set X-XSS-Protection "1; mode=block" Header set Referrer-Policy "strict-origin-when-cross-origin" Header set Permissions-Policy "camera=(), microphone=(), geolocation=()"
.htaccess file before making changes. An incorrect rule can cause a 500 Internal Server Error. If you do, simply remove the last rule you added.Nginx Firewall Configuration
For sites hosted on Nginx, here's the equivalent security configuration block that should be added to your server block:
# ââ Block sensitive files ââââââââââââââââ location ~ /wp-config\.php { deny all; } location ~ /xmlrpc\.php { deny all; } location ~ \.(ht|git|svn|env)$ { deny all; } # ââ Block directory listing ââââââââââââââ autoindex off; # ââ Block SQL injection & XSS ââââââââââââ if ($query_string ~* "(union.*select|