Repository: next.js
Issue: Rewrite with wildcard path is redirecting
Labels: bug

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
❯ next info

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:58 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6020
    Binaries:
      Node: 19.7.0
      npm: 9.5.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.3-canary.0
      eslint-config-next: 13.4.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
```


### Which area(s) of Next.js are affected? (leave empty if unsure)

_No response_

### Link to the code that reproduces this issue

https://github.com/jcity/next-bug

### To Reproduce

1. Clone example repo
 
2. Set `ADMIN_API_URL` (Try with both examples in the README)

3. Start server

4. Go to http://localhost:3000/api/admin/v1/alert/ in the browser and observe the wrong redirect. Or use curl to see the redirect headers (`curl -v http://localhost:3001/api/admin/v1/alert/`)

Example of curl output:
```zsh
❯ curl -v http://localhost:3000/api/admin/v1/alert/
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert/ HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 308 Permanent Redirect
< location: /api/admin/v1/alert
< refresh: 0;url=/api/admin/v1/alert
< date: Fri, 12 May 2023 18:40:43 GMT
< connection: close
< transfer-encoding: chunked
< 
* Closing connection 0
```


### Describe the Bug

## When using an *actual* URL (ex `https://some.api.com/dev`)

Next is redirecting the request instead of rewriting it to the correct URL for some paths. 

Examples:
* `/api/admin/v1/alert/` gets **redirected (308)** (NOT **rewritten**) to `ADMIN_API_URL/v1/alert/`
```zsh
❯ curl -v http://localhost:3000/api/admin/v1/alert/
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert/ HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 308 Permanent Redirect
< location: /api/admin/v1/alert
< refresh: 0;url=/api/admin/v1/alert
< date: Fri, 12 May 2023 19:08:34 GMT
< connection: close
< transfer-encoding: chunked
< 
```
* `/api/admin/v1/alert` gets **redirected (307)** (NOT **rewritten**) to `ADMIN_API_URL/v1/alert/`
```zsh
❯ curl -v http://localhost:3000/api/admin/v1/alert
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 307 Temporary Redirect
< content-type: application/json
< content-length: 0
< connection: close
< date: Fri, 12 May 2023 19:08:38 GMT
< x-amzn-requestid: 49294894-b8ca-4797-8e74-8f2e5d5edfa2
< x-amzn-remapped-content-length: 0
< x-amz-apigw-id: E0s8hG83PHcFjSQ=
< location: <REDACTED>
< x-amzn-trace-id: Root=1-645e8eb6-6340f0815b8358c17fbfaca9;Sampled=0;lineage=4986567b:0
< x-cache: Miss from cloudfront
< via: 1.1 9840972152ee9610a58198d320aa7c1a.cloudfront.net (CloudFront)
< x-amz-cf-pop: BCN50-P2
< x-amz-cf-id: FVWX-nmavnvzCxOMz_FuFsytWEDGMCv1lXoFBwgqMwFBbpajZRD3ww==
< vary: Accept-Encoding
< 
* Closing connection 0
```
* `/api/admin/v1/alert/all` is **CORRECTLY** rewritten to `ADMIN_API_URL/v1/alert/all`


## When using localhost (ex `http://localhost:8000`)

Next is redirecting for some requests and throwing a 500 for others.

Examples:
`/api/admin/v1/alert/`
```zsh
❯ curl -v http://localhost:3000/api/admin/v1/alert/
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert/ HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 308 Permanent Redirect
< location: /api/admin/v1/alert
< refresh: 0;url=/api/admin/v1/alert
< date: Fri, 12 May 2023 19:06:08 GMT
< connection: close
< transfer-encoding: chunked
< 
* Closing connection 0
```

`/api/admin/v1/alert`
```zsh
❯ curl -v http://localhost:3000/api/admin/v1/alert
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< cache-control: no-store, must-revalidate
< x-powered-by: Next.js
< etag: "qdwglgx92j16y"
< content-type: text/html; charset=utf-8
< content-length: 1546
< vary: Accept-Encoding
< date: Fri, 12 May 2023 19:04:56 GMT
< connection: close
< 
* Closing connection 0
```

`/api/admin/v1/alert/all`
```
❯ curl -v http://localhost:3000/api/admin/v1/alert/all
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/admin/v1/alert/all HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.87.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< cache-control: no-store, must-revalidate
< x-powered-by: Next.js
< etag: "2g35bqhscq172"
< content-type: text/html; charset=utf-8
< content-length: 1550
< vary: Accept-Encoding
< date: Fri, 12 May 2023 19:06:34 GMT
< connection: close
< 
* Closing connection 0
```

### Expected Behavior

All URLs should be rewritten with the correct path appended.

Examples:

`/api/admin/v1/alert` rewritten to `<ADMIN_API_URL>/v1/alert`
`/api/admin/v1/alert/` rewritten to `<ADMIN_API_URL>/v1/alert/`


### Which browser are you using? (if relevant)

Chrome

### How are you deploying your application? (if relevant)

Vercel