简体   繁体   English

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

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

I search a lot for this on the internet but I don't find any article related to it.我在互联网上搜索了很多,但我没有找到任何与之相关的文章。

Like I have a folder called pages in the root of my project and below tree is files of it.就像我在项目的根目录中有一个名为 pages 的文件夹,树下面是它的文件。

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

next.js gives default behavior when someone opens project.local:3000 it will open index.js and project.local:3000/app it will open app/index.js but I want that when someone open app.project.local:3000 it will open app/index.js . next.js 给出默认行为,当有人打开project.local:3000它将打开index.jsproject.local:3000/app它将打开app/index.js但我希望当有人打开app.project.local:3000它会打开app/index.js

My Hosts file我的主机文件

127.0.0.1 project.local
127.0.0.1 app.project.local

In short简而言之

I want to redirect pages/app folder to app.project.local or app.example.com in next.js我想将pages/app文件夹重定向到 next.js 中的app.project.localapp.example.com

I'm still a noobie in next.js (2nd day learning), but I was searching for subdomain support and I found three solutions on this Github issue: https://github.com/vercel/next.js/issues/5682我仍然是 next.js(第 2 天学习)中的菜鸟,但我正在寻找子域支持,我在这个 Github 问题上找到了三个解决方案: Z5E056C500A1C4B6A7110B50D807BADE.jsZissue://github.com/50D807BADE.5

  1. Using zones (still no idea how it works)使用区域(仍然不知道它是如何工作的)
  2. "Vercel will implement subdomain routing in the near future" (I don't expect to use Vercel in the near future) 《Vercel 近期会实现子域路由》(我不指望近期会使用 Vercel)
  3. (my preferred, but not yet tested) An example using custom servers in Next.js: https://github.com/dcangulo/nextjs-subdomain-example For #3, see how it was implemented in the server.js file (我的首选,但尚未测试)在 Next.js 中使用自定义服务器的示例: https://github.com/dcangulo/nextjs-subdomain-example对于#3,请参阅它是如何在server.js文件中实现的

Most updated solution最新的解决方案

I found the solution while exploring the documentation on redirects .我在探索有关重定向的文档时找到了解决方案。

In your Next.js's root folder, create a vercel.json file and then insert your redirects as object inside redirects array like so:在 Next.js 的根文件夹中,创建一个vercel.json文件,然后将重定向作为 object 插入到redirects数组中,如下所示:

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

This will only work on production environment.这仅适用于生产环境。 It should work as intended.它应该按预期工作。

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

You can specify a locale in your config, eg admin and specify the URL like this:您可以在配置中指定区域设置,例如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