Vercel.json - the "has" condition on "host" doesn't seem to work?

Hey guys,

I have a few subdomains set up and want to stream my traffic into different project directory based on the subdomain being visited.

A minimal version of my vercel.json looks like this -

{
“rewrites”: [
{
“source”: “/test”,
“destination”: “/test/$1”
},
{
“source”: “/(.*)”,
“has”: [{ “type”: “host”, “value”: “api.example.com” }],
“destination”: “/api/$1”
}
],
“trailingSlash”: false
}

So the trailingSlash rule and the /test rule are both working as expected.

The rules like the api for the subdomain matching don’t seem to work.

Expected behaviour -
api.example..com/test shall fetch /api/test and show a 404 error.

Actual behaviour -
api.example..com/test happily shows /test

I wonder if the has condition on host actually works or not. I thought I did everything right here. Please help if you have any ideas. Thx.

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

I think in your case, the /test rule is being processed before your subdomain rule, which is why api.example.com/test is showing /test instead of /api/test. Does this work?

{
  "rewrites": [
    {
      "source": "/(.*)",
      "has": [{ "type": "host", "value": "api.example.com" }],
      "destination": "/api/$1"
    },
    {
      "source": "/test",
      "destination": "/test/$1"
    }
  ],
  "trailingSlash": false
}

Sorry I raised a bad example. I was trying to show the rewrite works without the “has”.

In my real setup I only have subdomain routing, and all of them won’t work.

It’s just buggy. I think wrong with the nginx setup. Anyway I fallback to using middleware to control and reverted my vercel.json.