[英]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.js
和project.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.local
或app.example.com
我仍然是 next.js(第 2 天学习)中的菜鸟,但我正在寻找子域支持,我在这个 Github 问题上找到了三个解决方案: Z5E056C500A1C4B6A7110B50D807BADE.jsZissue://github.com/50D807BADE.5
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.