#rewrite from subdomain to subpath

1 messages · Page 1 of 1 (latest)

halcyon juncoBOT
#

Hi @somber comet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To achieve the behavior you described, you can use Vercel's rewrite functionality. This will allow you to serve content from mydomain.com/faq when the user visits faq.mydomain.com without changing the URL in the address bar.

Here's how you can configure this in your next.config.js file:

// next.config.js
module.exports = {
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/:path*',
          has: [
            {
              type: 'host',
              value: 'faq.mydomain.com',
            },
          ],
          destination: '/faq/:path*',
        },
      ],
    };
  },
};

This configuration ensures that any request to faq.mydomain.com will be rewritten to mydomain.com/faq while keeping the URL in the address bar unchanged.

Alternatively, if you are using a vercel.json file, you can configure the rewrite as follows:

// vercel.json
{
  "rewrites": [
    {
      "source": "/:path*",
      "has": [
        {
          "type": "host",
          "value": "faq.mydomain.com"
        }
      ],
      "destination": "/faq/:path*"
    }
  ]
}

This setup will achieve the same result, ensuring that requests to faq.mydomain.com are served from mydomain.com/faq without changing the visible URL.

For more details, you can refer to the Vercel documentation on rewrites (https://vercel.com/docs/project-configuration#project-configuration/rewrites).

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: