# Map Remote

> Send matching requests to a different scheme, host, port, path or query, such as staging or a local dev server, with a live preview of the rewritten URL.

Source: https://docs.subcloak.com/cloak-proxy/tools/map-remote/

Map Remote sends matching requests to a different server. You choose which parts of the URL to change (scheme, host, port, path or query) and leave the rest as the app sent it. Use it to point a production build at staging, send one API version to a local dev server while keeping the rest of each path, or compare two versions of an API without rebuilding anything.

## Send requests to another server

1. Right-click a request from the server you want to replace and choose **Tools › Map Remote…**.

   Cloak Proxy creates a rule that matches the request's URL (without its query string) for any method, with subpaths on, and fills **Host** with the current host. It turns Map Remote on and opens the rule.

2. Change the matcher if you want more of the API mapped. For a whole host, use `https://api.example.com/`.

3. Under **Send to**, fill in only the parts that should change. For staging, change **Host** to `staging.example.com`. (added in Cloak Proxy 0.5.1) To send everything under the pattern to a local server, type `http://localhost:8080/*` in **Fill in from a URL** and press Return: it fills in the scheme, host and port, and the trailing `*` sets **Path** to **Swap the prefix**.

4. Check the **Preview** line, or type a URL in the tester under the pattern (see [Test a rule](#test-a-rule)), then make the request again. The traffic list shows the new URL with a `Map Remote` badge.

*[Screenshot: A Map Remote rule matching https://api.shopapp.example/v1/* with scheme http, host localhost, port 8080 and Path set to Swap the prefix with /, and the tester showing https://api.shopapp.example/v1/orders/ord_1001?expand=items matches and becomes http://localhost:8080/orders/ord_1001?expand=items. Swap the prefix keeps the rest of the path and the query. The tester shows where a URL goes.]*

## Send to fields

Every field is optional. An empty field keeps that part of the original URL, as its placeholder says (**unchanged**).

| Field | Example | What it does |
|---|---|---|
| **Fill in from a URL** (added in Cloak Proxy 0.5.1) | `http://localhost:8080/*` | Fills in the fields below from a destination URL. A trailing `*` sets **Path** to **Swap the prefix**; without one, **Path** replaces the whole path. A URL without a scheme keeps the current **Scheme**. |
| **Scheme** | `http` | Switches between `http` and `https`. Entered in any case; sent in lowercase. |
| **Host** | `staging.example.com` | The server to connect to. A port after a colon (`localhost:3000`) is used when **Port** is empty. (added in Cloak Proxy 0.5.1) |
| **Port** | `3000` | A port number. When it's the default for the scheme (443 for HTTPS, 80 for HTTP) it's left out of the URL. |
| **Path: Replace the path** | `/v2/users` | Replaces the **whole** path. A missing leading `/` is added. |
| **Path: Swap the prefix** (added in Cloak Proxy 0.5.1) | `/` or `/v2/` | Replaces only the start of the path that the rule's pattern spells out (`/v1/` for `https://api.example.com/v1/*`) and keeps the rest and the query. Empty means `/`. |
| **Query** | `debug=1` | Replaces the **whole** query string. A leading `?` is optional. |
| **Keep the original Host header** | Off | When off, the `Host` header is set to the new host (and port, if it isn't the default). When on, the app's original `Host` header is sent to the new server. |

### Examples

For the request `https://api.example.com/v1/users?page=2`:

| You set | The request goes to |
|---|---|
| **Host** `staging.example.com` | `https://staging.example.com/v1/users?page=2` |
| **Scheme** `http`, **Host** `localhost`, **Port** `3000` | `http://localhost:3000/v1/users?page=2` |
| **Path** `/v2/users` | `https://api.example.com/v2/users?page=2` |
| **Query** `debug=1` | `https://api.example.com/v1/users?debug=1` |
| **Path** `/v2/`, **Swap the prefix** (rule `api.example.com/v1/*`) | `https://api.example.com/v2/users?page=2` |
| **Scheme** `http`, **Host** `localhost`, **Port** `8080`, **Path** `/`, **Swap the prefix** | `http://localhost:8080/users?page=2` |

### Swap the prefix

(added in Cloak Proxy 0.5.1)

With **Path** set to **Swap the prefix**, the part of the path the pattern spells out before its first `*` is swapped for **Path**, and the rest of the path and the query are kept. For a rule matching `https://api.example.com/v1/*`:

| Request | **Path** `/` | **Path** `/v2` |
|---|---|---|
| `/v1/users?page=2` | `/users?page=2` | `/v2/users?page=2` |
| `/v1/users/42/orders` | `/users/42/orders` | `/v2/users/42/orders` |
| `/v1/` and `/v1` | `/` | `/v2/` and `/v2` |

The prefix is compared without regard to case, like the pattern. A pattern whose prefix doesn't end in `/` (`api.example.com/v1*`) swaps text: `/v1beta/x` with **Path** `/v2` becomes `/v2beta/x`. A wildcard in the host (`*.example.com/static/*`) doesn't change the prefix. With a regular expression, there's no literal prefix, so the whole path is kept after **Path**: `/proxy` sends `/users` to `/proxy/users`.

With **Replace the path**, **Path** replaces the entire path: a rule matching `api.example.com/v1/*` with **Path** `/v2/users` sends every `/v1/…` request to `/v2/users`. Up to 0.5.0, that was the only choice.

> **Query replaces:**
> **Query** replaces every query parameter. To add or change one parameter, use a [Rewrite](https://docs.subcloak.com/cloak-proxy/tools/rewrite/) rule.

### Preview

Below the fields, **Preview** shows an example URL built from your matcher (each `*` replaced by `example`, and `https://` added when the pattern has no scheme), struck through, and under it the URL Map Remote would send. It updates as you type.

### Test a rule

(added in Cloak Proxy 0.5.1)

Type a URL in the tester under the pattern, or pick a captured request from **Captured**, to see whether the rule matches it and, for Map Remote, the URL it's sent to. See [Test a pattern](https://docs.subcloak.com/cloak-proxy/tools/rules-and-matching/#test-a-pattern).

## Mirror instead of redirect

(added in Cloak Proxy 0.4.0)

Under **When a request matches**, **Send it there instead** (the default) redirects the request. **Send a copy there and compare** leaves the request alone — it goes to its own host as usual — and, once it has finished, sends a copy to the rule's destination and compares the two answers in [Compare Environments](https://docs.subcloak.com/cloak-proxy/testing/compare-environments/#mirror-live-traffic). Use it to check that staging answers like production while you use the app. Writes aren't copied unless you allow them there, and copies are rate-limited.

## When to keep the Host header

Most servers need a `Host` header that matches their own name, so leave **Keep the original Host header** off. Turn it on when the new server routes by the original name, for example:

- A load balancer or reverse proxy at an IP address that serves several sites.
- A local server that expects the production host name.

To keep the URL and Host header and only change which machine you connect to, [DNS Spoofing](https://docs.subcloak.com/cloak-proxy/tools/dns-spoofing/) is often simpler.

## How Map Remote works with other tools

- Map Remote matches the request after request scripts and the request breakpoint, and runs after [Map Local](https://docs.subcloak.com/cloak-proxy/tools/map-local/) and [Mock Sets](https://docs.subcloak.com/cloak-proxy/tools/mock-sets/). If either answers the request, Map Remote doesn't run.
- Only the first matching Map Remote rule is used.
- URLs that contain characters such as `|`, `{` or spaces in the query are mapped like any other; their path and query are sent as the app wrote them. (added in Cloak Proxy 0.5.1)
- [DNS Spoofing](https://docs.subcloak.com/cloak-proxy/tools/dns-spoofing/) and [No Caching](https://docs.subcloak.com/cloak-proxy/tools/no-caching/) apply to the rewritten request.
- Response scripts and the response breakpoint still match the original URL. [Alerts](https://docs.subcloak.com/cloak-proxy/tools/alerts/) match the rewritten URL, which is also what the traffic list shows.

> **Certificates:**
> Cloak Proxy checks the new server's HTTPS certificate against its host name, as for any request. If a local or staging server uses a self-signed certificate, the request fails with a certificate error. Add that host to the insecure upstream hosts in [Network settings](https://docs.subcloak.com/cloak-proxy/capture/network-settings/), or switch the rule's **Scheme** to `http` if the server supports it.

> **Caution:**
> A Map Remote rule keeps sending traffic to the other server until you turn it off, even after a restart. Before you test against production again, hover **N tools on** in the bottom bar to check that **Map Remote** isn't active.

## Related

  - [DNS Spoofing](https://docs.subcloak.com/cloak-proxy/tools/dns-spoofing/): Connect a host to another IP without changing the URL.
  - [Map Local](https://docs.subcloak.com/cloak-proxy/tools/map-local/): Answer requests from your Mac instead of a server.
  - [Rules and matching](https://docs.subcloak.com/cloak-proxy/tools/rules-and-matching/): Patterns, subpaths and the order tools run in.
  - [Network settings](https://docs.subcloak.com/cloak-proxy/capture/network-settings/): Insecure upstream hosts, external proxies and more.
