Restrict WP API access to a list of approved IP address ranges via .htaccess
-
We are planning on using WordPress REST API v2 and we would like to restrict WP API access to a list of approved IP address ranges. We will have other systems in our stack talking to our wordpress instances, but we don’t want to expose our wp api endpoints to the world. I understand that we’ll need to setup authentication to secure our wp api calls, but we would like to add another layer to restrict access to internal systems only.
For my testing, my .htaccess has this rule to outright block /wp-json/ URLs for testing purpose.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^wp-json.* – [F]
</IfModule>But I am still able to access /wp-json/ via browser.
I have no issue with other wp urls such as this rule to block all wp-admin URLs:
RewriteRule ^wp-admin.* – [F]
I plugged in my test strings to https://htaccess.madewithlove.be/ to test my htaccess rule and according to them, the rule was validated.
(https://localhost:8080/wp-json/wp/v2/pages/?filter%5Bname%5D=test-page)I am wondering if wp core or wp api does any special redirect/filter for /wp-json/ url that might prevent .htaccess rules to work?
Any recommendations on how we could implement wp api access restriction to a list of approved IP addresses?
- The topic ‘Restrict WP API access to a list of approved IP address ranges via .htaccess’ is closed to new replies.