.htaccess File WordPress IP Restriction Not Working: Causes and Solutions

 

The .htaccess file is a powerful configuration tool used on Apache web servers to control access, redirection, URL rewriting, and security. One common use case is restricting access to a WordPress site based on IP address — especially for the admin area (wp-admin) or login page (wp-login.php).

But sometimes, IP restriction rules in .htaccess don’t work as expected. This article explores why your IP restriction in .htaccess may not be working and how to fix it.


Common Use Case: IP Restriction in .htaccess

Here’s a typical .htaccess rule to allow access only from a specific IP address:

apache
<Files wp-login.php> Order Deny,Allow Deny from all Allow from 123.456.78.90 </Files>

Or for the entire admin directory:

apache
<Directory /wp-admin> Order Deny,Allow Deny from all Allow from 123.456.78.90 </Directory>

Yet despite adding these rules, users often report they can still access the site — or get locked out even when using the correct IP. Why?


Why IP Restrictions in .htaccess Might Not Work

1. Wrong IP Address (Public vs. Private)

  • You may be using your local/private IP (e.g., 192.168.x.x) instead of your public IP.

  • Visit whatismyip.com to find your correct public IP address.

2. Cloudflare or CDN in Use

  • If you're using Cloudflare, Sucuri, or another CDN, the server may see the CDN’s IP instead of the visitor's real IP.

  • Solution: Configure your server or .htaccess to read the real IP from headers like CF-Connecting-IP.

Example:

apache
SetEnvIf X-Forwarded-For "^123\.456\.78\.90$" allow_ip Order Deny,Allow Deny from all Allow from env=allow_ip

3. Apache Version or Configuration

  • The Order, Allow, and Deny directives work in Apache 2.2.

  • In Apache 2.4, these are replaced by Require directives.

Apache 2.4 Syntax:

apache
<Files wp-login.php> Require ip 123.456.78.90 </Files>

Block everyone except your IP:

apache
<Files wp-login.php> Require all denied Require ip 123.456.78.90 </Files>

4. Wrong File or Directory Placement

  • .htaccess rules must be placed in the root directory of your WordPress site or the correct subdirectory (e.g., /wp-admin/).

  • If placed incorrectly, they won’t affect the intended file or directory.

5. Missing or Incorrect .htaccess File Permissions

  • If the .htaccess file is not readable by the server, the rules won’t apply.

  • Correct permission is usually 644.

6. Conflicts with Other Plugins or Rules

  • Security plugins like Wordfence or All In One WP Security may override .htaccess settings.

  • Check for conflicting rules or plugin-based firewall settings.


How to Test If IP Restriction Works

  1. Apply your .htaccess rule.

  2. Visit the restricted page from your allowed IP — it should work.

  3. Use a proxy or VPN to simulate another IP — it should be blocked.

  4. Check error logs (/var/log/apache2/error.log) for clues if issues persist.


Additional Tip: Whitelist Multiple IPs

apache
<Files wp-login.php> Require all denied Require ip 123.456.78.90 Require ip 111.222.33.44 </Files>

Conclusion

If your WordPress .htaccess IP restriction is not working, the issue likely stems from outdated syntax, wrong IPs, CDN interference, or server misconfiguration. By identifying your server environment and using the correct directives, you can effectively restrict access to sensitive areas of your WordPress site and enhance security.

Comments

Popular posts from this blog

JavaScript Fusker: What It Is and Why You Should Care

How Did You Hear About Us? Understanding the Importance Behind the Question

Vents Content: The Rise of Digital Emotional Expression