繁体   English   中英

如何将页面/应用程序文件夹重定向到 next.js 中的子域

[英]How to redirect pages/app folder to subdomain in next.js

我在互联网上搜索了很多,但我没有找到任何与之相关的文章。

就像我在项目的根目录中有一个名为 pages 的文件夹,树下面是它的文件。

|   404.js
|   auth.js
|   index.js
|   _app.js
|   _error.js
\---app
        index.js

next.js 给出默认行为,当有人打开project.local:3000它将打开index.jsproject.local:3000/app它将打开app/index.js但我希望当有人打开app.project.local:3000它会打开app/index.js

我的主机文件

127.0.0.1 project.local
127.0.0.1 app.project.local

简而言之

我想将pages/app文件夹重定向到 next.js 中的app.project.localapp.example.com

我仍然是 next.js(第 2 天学习)中的菜鸟,但我正在寻找子域支持,我在这个 Github 问题上找到了三个解决方案: Z5E056C500A1C4B6A7110B50D807BADE.jsZissue://github.com/50D807BADE.5

  1. 使用区域(仍然不知道它是如何工作的)
  2. 《Vercel 近期会实现子域路由》(我不指望近期会使用 Vercel)
  3. (我的首选,但尚未测试)在 Next.js 中使用自定义服务器的示例: https://github.com/dcangulo/nextjs-subdomain-example对于#3,请参阅它是如何在server.js文件中实现的

最新的解决方案

我在探索有关重定向的文档时找到了解决方案。

在 Next.js 的根文件夹中,创建一个vercel.json文件,然后将重定向作为 object 插入到redirects数组中,如下所示:

{
  "redirects": [
    { "source": "/blog", "destination": "https://blog.example.com" }
  ]
}

这仅适用于生产环境。 它应该按预期工作。

NextJS 现在支持语言环境: https://nextjs.org/docs/advanced-features/i18n-routing

您可以在配置中指定区域设置,例如admin并指定 URL,如下所示:

// next.config.js
module.exports = {
  i18n: {
    // These are all the locales you want to support in
    // your application
    locales: ['en-US', 'admin', 'nl-NL'],
    // This is the default locale you want to be used when visiting
    // a non-locale prefixed path e.g. `/hello`
    defaultLocale: 'en-US',
    // This is a list of locale domains and the default locale they
    // should handle (these are only required when setting up domain routing)
    // Note: subdomains must be included in the domain value to be matched e.g. "fr.example.com".
    domains: [
      {
        domain: 'example.com',
        defaultLocale: 'en-US',
      },
      {
        domain: 'example.nl',
        defaultLocale: 'nl-NL',
      },
      {
        domain: 'admin.example.com',
        defaultLocale: 'admin',
        // an optional http field can also be used to test
        // locale domains locally with http instead of https
        http: true,
      },
    ],
  },
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM