#Rewrite subpaths to subdomains

1 messages · Page 1 of 1 (latest)

royal wadiBOT
#

Hi @wide trout. 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 desired URL structure where subpaths are rewritten to subdomains, you can use the rewrites feature in Next.js. This involves configuring your next.config.js file to handle the rewrites based on the host.

Here's how you can set it up:

  1. Create or update your next.config.js file:
    • Use the rewrites function to specify the source paths and the conditions under which they should be rewritten.
    • Use the has field to check for the host and rewrite the paths accordingly.

Here is an example configuration:

// next.config.js
module.exports = {
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/new',
          has: [
            {
              type: 'host',
              value: 'google.mydomain.com',
            },
          ],
          destination: '/team/google/new',
        },
        {
          source: '/last',
          has: [
            {
              type: 'host',
              value: 'vercel.mydomain.com',
            },
          ],
          destination: '/team/vercel/last',
        },
      ],
    };
  },
};

In this configuration:

  • Requests to google.mydomain.com/new will be rewritten to www.mydomain.com/team/google/new.
  • Requests to vercel.mydomain.com/last will be rewritten to www.mydomain.com/team/vercel/last.
  1. Deploy your changes:
    • Push your changes to your repository and redeploy your application on Vercel.

For more detailed information on configuring rewrites, you can refer to the Vercel documentation on rewrites (https://vercel.com/guides/can-i-redirect-from-a-subdomain-to-a-subpath).

#

If you need to configure subdomains, you can do so either with a CNAME record or through the Nameservers method as described in the Vercel documentation on adding and configuring a custom domain (https://vercel.com/docs/projects/domains/add-a-domain).

By following these steps, you should be able to rewrite your subpaths to subdomains as required.

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