Fix REST API for WordPress Hosted on Different Domains

After hosting the WordPress admin panel on a different domain, the Site Health tool tells me that REST API isn’t working properly. I use the classic editor and widgets so this problem doesn’t affect me much. But since this problem shows up, I decided to find a fix for it. Luckily, it wasn’t too complicated to solve this problem.

The Site Health tool shows this:

The REST API encountered an unexpected result

The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages.
When testing the REST API, an unexpected result was returned:

REST API Endpoint:
REST API Response: (403) Forbidden

We are on the “” site, but REST API Endpoint still uses the “” domain. That is the problem. What we need is to change the REST API Endpoint URL, replacing “www” with “admin”.

The 403 Forbidden error from the “www” site was an “expected result” since we didn’t log in to the “www” site after all.

A look into the “rest-api.php” file reveals that REST URLs are based on home_url (the “WP_HOME”) instead of site_url (the “WP_SITEURL” we set). We can use the “rest_url” filter to replace home_url with site_url.

Add these PHP codes to your WordPress site:

// Fix WordPress REST API - Replace home_url with site_url
add_filter( 'rest_url', 'draftposts_resturl_replace_homeurl_with_siteurl' );
function draftposts_resturl_replace_homeurl_with_siteurl( $url ) {
	return str_replace( home_url(), site_url(), $url);
// END

Now test again. Problem solved.

Leave a Comment