Subdomain root can not be redirected to subpath

I want to redirect subdomain to subpath of my project.
the rewrite rule in the vercel.json is below:

{
  "rewrites": [
    {
      "source": "/:path*",
      "has": [
        {
          "type": "host",
          "value": "app.acme.com"
        }
      ],
      "destination": "/app/index.html"
    }
  ]
}

result:

  1. app.acme.com/anypath can be redirected to the “destination”: “/app/index.html”

  2. app.acme.com can not be redirected to the “destination”: “/app/index.html”.

I want to redirected app.acme.com to the “destination”: “/app/index.html”.

Is there issue in the vercel.json?

Hi, @webstarue-gmailcom! Welcome to the Vercel Community :blush:

Thank you for sharing your vercel.json configuration. I’ve reviewed it, and it appears that the current rewrite rule should actually work for both the root path (app.acme.com) and any subpaths (app.acme.com/anypath). The /:path* pattern in the source field is designed to match both scenarios.

However, since you’re experiencing an issue with the root path not being redirected, there might be other factors to consider:

  • Try clearing your browser cache or testing in an incognito/private browsing window.
  • Ensure you’ve redeployed your project after updating the vercel.json file.
  • Check for any other routing rules in your application that might be overriding this rewrite.
  • Sometimes, changes may take a short while to propagate across Vercel’s Edge Network.

If you’re still facing issues after considering these factors, you could try an alternative configuration that explicitly handles both cases:

{
  "rewrites": [
    {
      "source": "/",
      "has": [
        {
          "type": "host",
          "value": "app.acme.com"
        }
      ],
      "destination": "/app/index.html"
    },
    {
      "source": "/:path*",
      "has": [
        {
          "type": "host",
          "value": "app.acme.com"
        }
      ],
      "destination": "/app/index.html"
    }
  ]
}

This configuration explicitly handles both the root path and subpaths.

Remember that rewrites are different from redirects:

  • With rewrites, the URL in the browser remains unchanged, but the content served comes from the destination path.
  • If you want to change the visible URL, you would need to use the redirects configuration instead of rewrites .

Let us know how you get on!

Hi, @pawlean ! Thank you very much for your response.
I have implemented all your suggestions and now still can’t access ‘/app/index.html’ through the root path ‘app.acme.com’ even use the alternative configuration.
Is there any way to debug this issue?
Looking forward your response.
Than you and have a nice day!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.