Skip to content

Locking the WordPress admin area to specific IPs on cPanel

How to restrict access to wp-admin and wp-login.php by IP on a cPanel server using .htaccess, plus the trade-offs against plugin-based and Cloudflare-based approaches.

If /wp-login.php accepts requests from anywhere on the public internet, half of your security posture is hoping nobody guesses the password. Locking that endpoint to a known set of IPs is one configuration change away. The trade-off is real — VPN, office moves, working from a hotel — so think about who actually needs to log in before you do this.

What’s happening

WordPress doesn’t know who’s allowed to log in until they try. Brute-force, credential stuffing, and dictionary attacks all hit /wp-login.php and /xmlrpc.php, the application then responds. Restricting access at the web server level means the application is never asked. Apache and LiteSpeed both honor .htaccess directives that filter by source IP. The directive runs before WordPress loads.

How to verify

Identify the IPs that need to keep working: office static IP, on-call engineer VPN exit, third-party integrations that POST to admin endpoints (rare, but check). Then count what you’d lose:

grep -E 'POST /wp-login.php' /home/cpaneluser/access-logs/example.com | \
  awk '{print $1}' | sort | uniq -c | sort -rn | head -20

The IPs you want to keep should be in that list. Everything else is either an attacker or unused.

The fix

  1. Create a dedicated .htaccess block in the wp-admin folder and a separate rule for wp-login.php at the document root. Inside ~/public_html/wp-admin/.htaccess:

    <RequireAll>
        Require ip 203.0.113.0/24
        Require ip 198.51.100.42
        Require ip ::1
    </RequireAll>

    Or the Apache 2.2-compatible form (still works on most cPanel hosts):

    Order Deny,Allow
    Deny from all
    Allow from 203.0.113.0/24
    Allow from 198.51.100.42
  2. For wp-login.php at the document root, the file-scoped form:

    <Files "wp-login.php">
        <RequireAll>
            Require ip 203.0.113.0/24
            Require ip 198.51.100.42
        </RequireAll>
    </Files>
  3. Whitelist the admin-ajax.php exception. Many front-end features call admin-ajax.php and rejecting them breaks the public site:

    <Files "admin-ajax.php">
        Require all granted
    </Files>
  4. Test from a non-whitelisted network. You should get a 403. Test from the whitelisted IP — you should reach the login screen. If you locked yourself out, log into cPanel via the panel (not the WordPress admin), File Manager, and edit the .htaccess.

  5. If the site is behind Cloudflare, the IP Apache sees is the Cloudflare edge unless you have mod_cloudflare or the equivalent restoring the real client IP. Either install that and restart Apache, or apply the IP allowlist at the Cloudflare WAF instead (Custom Rules > Block requests where Path matches ^/wp-(login|admin) AND IP not in {your list}).

  6. Document the whitelist in the runbook. When an admin’s IP changes, they need a fast path to update — not a 90-minute scramble.

For environments where this configuration sits on top of a larger WAF and brute-force strategy, Stack Harbor handles it under enterprise WordPress hosting.