Only expose routes with prefix /wp-json on WordPress using Apache
Pretty straightforward:
I want to only expose routes with the /wp-json prefix so that I can use headless WordPress without exposing the rest of my WordPress site to the internet.
I'm running this on Apache, but haven't yet figured out the proper syntax for .htaccess.
So far I have tried:
Directory /
AllowOverride None
/Directory
Directory /wp-json*
AllowOverride All
/Directory
Directory /wp-json*/*
AllowOverride None
/Directory
and
Directory /wp-json/
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from all
/Directory
My .htaccess is located inside of the public_html
root directory. I'm not sure if I've got the .htaccess in the wrong directory or, if the syntax I'm using just isn't working. Any tips?
EDIT:
MrWhite's example worked. I ultimately added another rewrite rule for /wp-admin and my final .htaccess
is as follows (note the [F]
changes to a [C]
on the wp-json RewriteRule):
RewriteEngine on
# Reject any user requests that are not prefixed /wp-json
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !wp-json($|/) - [C]
RewriteRule !wp-admin($|/) - [F]
# BEGIN WordPress
# The directives (lines) between BEGIN WordPress and END WordPress are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
IfModule mod_rewrite.c
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule
# END WordPress