title: GCP: load balancer rewrite path
body: <p>I'm setting up traffic management for a global external HTTP(S) load balancers. I have two backend Cloud Run services, <code>serverless-lb-www-service</code> and <code>serverless-lb-api-service</code>, that I want to serve from the same IP/domain.</p>
  <p>I want to configure them like this:</p>
  <p><code>example.com</code> -&gt; <code>serverless-lb-www-service</code></p>
  <p><code>example.com/api</code> -&gt; <code>serverless-lb-api-service</code></p>
  <p>I can use the simple routing rules to serve traffic <em>semi-expected</em>:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>path</th>
  <th>backend</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>/*</td>
  <td>serverless-lb-www-service</td>
  </tr>
  <tr>
  <td>/api</td>
  <td>serverless-lb-api-service</td>
  </tr>
  <tr>
  <td>/api/*</td>
  <td>serverless-lb-api-service</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>However, I'm running into an issue where I try to access an endpoint that is not the root API end, like <code>example.com/api/test</code>. I'm always seeing the response I would expect from <code>example.com/api</code>.</p>
  <p>I believe it has something to do with my API (running express.js) receiving the path <code>/api</code> when it is instead expecting to serve that route from there just <code>/test</code>. I think I might need to set up a rewrite to remove <code>/api</code> when it hits the API</p>
  <p>Any help would be much appreciated. Thanks</p>
  <p><strong>update</strong></p>
  <p>I can confirm that the requests as logged in the API are all prefixed with <code>/api</code>. I can solve my issue by changing all API route handlers to expect the <code>/api</code> prefix in production environment. However I would still rather do this via a path rewrite so application code is the same in all environments</p>