Ignas Damunskis Ignas Damunskis - 3 months ago 17
PHP Question

Wordpress - rewrite or hide wp-json in url

I would like to hide

/wp-json/
from url since I don't want anyone to know that website is built on wordpress. I tried rewriting rules in
.htaccess
, but
/api
only redirects to
/wp-json
so this is not what I trully want.

RewriteRule ^api/(.*)$ /wp-json/$1 [R,L]


These are not working at all:

RewriteRule ^api$ /wp-json [L]
RewriteRule ^api/(.*)$ /wp-json/$1 [L]


I also tried site_url hook event, but unfortunately it is not working at all:

add_filter('site_url', 'api_filter', 10, 3);
function api_filter($url, $path, $orig_scheme) {
$old = array('/(wp-json)/');
$new = array('api');
return preg_replace($old, $new, $url, 1);
}

Answer

You can use the filter rest_url_prefix to do the rewrite:

add_filter( 'rest_url_prefix', function() {
    return 'api';
});

Remember to visit Settings->Permalinks to flush the permalinks after adding the above code to functions.php

Comments