Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 658
Ignas Damunskis Avatar asked Aug 17 '16 11:08

Ignas Damunskis


People also ask

Should I block WP JSON?

The WordPress REST API provides endpoints for WordPress data types. This allows developers to interact with sites remotely by sending and receiving JSON objects. However, most website owners do not need these features, and it may be smarter to disable the WordPress JSON REST API.

How do I disable REST API in WordPress?

More precisely, it allows you to specify which routes can be accessed by unauthenticated users or users with specific user roles. To set this, navigate to Settings > Disable REST API. Then choose the appropriate user type with the Rules for: option and set the rules you want in the Manage Rules section below.

How do I restrict WordPress API?

Restrict Access to WordPress REST API To disable the WordPress REST API for the anonymous users, you can require authentication for all REST API requests by adding an is_user_logged_in check to the rest_authentication_errors filter in a child theme's functions. php file.


1 Answers

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

like image 151
trysmudford Avatar answered Oct 06 '22 00:10

trysmudford