WP Multisite - Additional subdomain on the site for API purposes

We have WP multisite network with WooCoomerce shops. All the network is served through Cloudflare CDN.

Our ERP is accessing WooCommerce API of each website in the WP network. Since there so many, calls we are getting 503 from Cloudflare. It's not a server problem. It's a problem of too many requests to Cloudflare.

So what I would like to have is a separated subdomain (example: api.domain.com) for each domain in the network. That subdomain will not be proxied thru Cloudflare.

How to do I do that? How can I add an additional subdomain to a site on WP multisite network?

Topic cloudflare multisite api Wordpress

Category Web


I have resovled it myself.

I have enabled sunrise in wp-config. Then add a script to sunrise.php to override HTTP_HOST.

I made it dynamic since we have many websites. When you visit api.domainxy.com it automatically shows you domainxy.com.

<?php
$re = '/(?:api\.)(.*)/m';
$str = $_SERVER['HTTP_HOST'];


preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

if ($matches && $matches[0][1] != "") {
  $_SERVER['HTTP_HOST'] = $matches[0][1];
}
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.