WordPress GuideSecurity → Restrict WordPress IP

How to restrict a WordPress IP with the .htaccess file

Want to block specific IPs or limit access to your WordPress admin area? The .htaccess file lets you do that without a plugin. Here’s how to get it right.

What is the .htaccess file in WordPress?

The .htaccess file is a configuration file used by Apache web servers, including most WordPress hosting environments. It controls key server-level functions like redirects, permalink structures, and access permissions.

In WordPress, you’ll find it in the root directory—typically public_html. With the right rules in place, you can block IP addresses, restrict access to sensitive files, or lock down your admin area. It’s a powerful tool, but make sure to edit it carefully.

1. Locate and edit your .htaccess file safely

Before making changes, always back up the file:

Once you’re in, you’re ready to start customizing access.

2. Block a specific IP address from accessing your site

To block a known malicious or spammy IP from viewing your entire website, add the following code to .htaccess:

Here’s what each line does:

You can add multiple deny from lines to block several IPs.

3. Allow only specific IPs to access the wp-admin directory

Locking down the admin area is a strong security measure, especially if your team uses a static IP.

Option 1: Add this to your site’s main .htaccess:

This only protects the wp-login.php file, which is the WordPress login form. It doesn’t restrict access to other parts of /wp-admin/.

Use case: When you want to block access to the login screen but still allow the dashboard assets (like admin-ajax.php) to load for certain frontend functionality (e.g., AJAX features).

Option 2: Create a separate .htaccess inside the /wp-admin/ folder:

This locks down the entire admin area. Any request to a file within /wp-admin/ is blocked unless it’s from an allowed IP.

Use case: When you want to harden access to the entire admin interface, not just the login page. However, it may block legitimate AJAX calls from the front end unless exceptions are added.

4. How to block an IP from accessing wp-login.php

Sometimes you only want to prevent login attempts, not full site access. To block an IP from just the login page:

This prevents brute-force attacks or bot login attempts while leaving your content publicly accessible.

5. Use CIDR notation to block IP ranges

If you’re dealing with a bot network or a known bad IP block, CIDR lets you deny entire IP ranges:

deny from 192.168.1.0/24

That example blocks all addresses from 192.168.1.0 to 192.168.1.255. Use this sparingly—it can unintentionally block real users, especially on mobile networks.

What happens when you block an IP?

Blocked users won’t get a polite message. They’ll just see a plain 403 Forbidden error when trying to access your site or the restricted areas.

This happens silently. There’s no alert to the blocked user, and your site works normally for everyone else. That makes .htaccess blocking ideal for cutting off known offenders without disrupting traffic.

Tip: Use comments to stay organized in your .htaccess file

It’s easy to lose track of changes in .htaccess. Add comments to label what each rule is doing:

Comments start with # and don’t affect functionality. This helps you avoid mistakes and makes your rules easier to review later.

Common mistakes and troubleshooting tips

Even a tiny typo in .htaccess can take down your whole site. Here’s how to avoid disaster:

Tip: If your host supports .htaccess overrides, you can use an .htaccess inside /wp-admin/ or other directories for localized rules.

Bonus: How to whitelist your dynamic IP with a DNS service

If your IP address changes often (which is common with home ISPs or mobile connections), use Dynamic DNS (DDNS) to manage access:

Allow from yoursubdomain.no-ip.org

This gives you a secure, flexible way to restrict admin access without needing a static IP.

Additional resources

Comprehensive guide to securing WordPress with ModSecurity

This guide provides a comprehensive overview of how to use ModSecurity to enhance the security of your WordPress site.

How to restrict a WordPress IP with the .htaccess file →

Protect your WordPress site from MIME-type attacks by preventing content sniffing in browsers.

Why security matters for WordPress enterprise hosting

Use the blog as your guide to attacks to watch out for, security best practices, and steps to improve the WordPress protection you already have.